Skip to content

Change dictionary to list of tuples to permit duplicate keys #19666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/matplotlib/backends/qt_editor/figureoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,21 @@ def figure_edit(axes, parent=None):

# Sorting for default labels (_lineXXX, _imageXXX).
def cmp_key(label):
if type(label) == tuple:
label = label[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the function somewhat awkward. In such a case, you should at least document what label can be.

But checking the code, it turns out that cmp_key() is only used another time for mappabledict. It suffers from the same duplicate label problem and should be changed to a tuple list as well. This will then make the type distinction unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, I will do it.

match = re.match(r"(_line|_image)(\d+)", label)
if match:
return match.group(1), int(match.group(2))
else:
return label, 0

# Get / Curves
linedict = {}
labeled_lines = []
for line in axes.get_lines():
label = line.get_label()
if label == '_nolegend_':
continue
linedict[label] = line
labeled_lines.append((label, line))
curves = []

def prepare_data(d, init):
Expand Down Expand Up @@ -101,9 +103,8 @@ def prepare_data(d, init):
sorted(short2name.items(),
key=lambda short_and_name: short_and_name[1]))

curvelabels = sorted(linedict, key=cmp_key)
for label in curvelabels:
line = linedict[label]
sorted_labels_and_curves = sorted(labeled_lines, key=cmp_key)
for label, line in sorted_labels_and_curves:
color = mcolors.to_hex(
mcolors.to_rgba(line.get_color(), line.get_alpha()),
keep_alpha=True)
Expand Down Expand Up @@ -205,7 +206,7 @@ def apply_callback(data):

# Set / Curves
for index, curve in enumerate(curves):
line = linedict[curvelabels[index]]
line = labeled_lines[index][1]
(label, linestyle, drawstyle, linewidth, color, marker, markersize,
markerfacecolor, markeredgecolor) = curve
line.set_label(label)
Expand Down