From 526ab0148a34e92ecdbd6a5d1195361801003d24 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 16 Feb 2026 05:17:44 -0600 Subject: [PATCH] scripts: plot[mpl].py: Merge legend labels if they would be identical This tweaks how we build legends in plot.py and plotmpl.py to merge legend labels if they would end up identical (same label, same color, same format, char, linechar, etc). Identical labels are confusing anyways, so we might as well minimize the size of the legend when this happens. --- Though the real motivation for this is to simplify legend labels that span multiple subplots. Before, you had to awkwardly glob out subplot labels you didn't want repeated in the legend: ./scripts/plot.py test.csv \ -L3,readed=lfs3 \ -L3,progged= \ -L3,erased= \ -L2,readed=lfs2 \ -L2,progged= \ -L2,erased= But now you can specify them willy-nilly, and in the final legend any redundant labels will be automatically merged: ./scripts/plot.py test.csv \ -L3=lfs3 \ -L2=lfs2 --- scripts/plot.py | 5 ++++- scripts/plotmpl.py | 24 ++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/scripts/plot.py b/scripts/plot.py index 158d94e0..fc3f12d2 100755 --- a/scripts/plot.py +++ b/scripts/plot.py @@ -1531,7 +1531,10 @@ def main_(ring, csv_paths, *, if name in datalabels_ else ','.join(name)) - if label: + # append and merge identical labels + if not label: + continue + if (label, datacolors_[name]) not in legend_: legend_.append((label, datacolors_[name])) legend_width = max(legend_width, len(label)+1) diff --git a/scripts/plotmpl.py b/scripts/plotmpl.py index 43f318cb..b3fe3f59 100755 --- a/scripts/plotmpl.py +++ b/scripts/plotmpl.py @@ -1334,14 +1334,26 @@ def main(csv_paths, output, *, legend[l] = h # sort in dataset order legend_ = [] + legend__ = set() for i, name in enumerate(datasets_.keys()): + if name in datalabels_ and not datalabels_[name]: + continue + name_ = ','.join(name) - if name_ in legend: - if name in datalabels_: - if datalabels_[name]: - legend_.append((datalabels_[name], legend[name_])) - else: - legend_.append((name_, legend[name_])) + if name_ not in legend: + continue + + if name in datalabels_: + label = datalabels_[name] + else: + label = name_ + + # append and merge identical labels + if not label: + continue + if (label, datacolors_[name], dataformats_[name]) not in legend__: + legend_.append((label, legend[name_])) + legend__.add((label, datacolors_[name], dataformats_[name])) legend = legend_ if legend_right: