Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2679d7

Browse files
committed
MNT: Minor cleanup of label formatting in PathCollection.legend_elements
1 parent 49fd492 commit e2679d7

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

lib/matplotlib/collections.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,15 @@ def legend_elements(self, prop="colors", num="auto",
12711271
"alpha": self.get_alpha(),
12721272
**kwargs}
12731273

1274-
for val, lab in zip(values, label_values):
1274+
# Pre-format all labels
1275+
# TODO: check whether we can switch to
1276+
# formatted_labels = fmt.format_ticks(label_values)
1277+
# There are subtle differences for some formatters as that passes the
1278+
# indices to __call__ as well.
1279+
fmt.set_locs(label_values)
1280+
formatted_labels = [fmt(lab) for lab in label_values]
1281+
1282+
for val, formatted in zip(values, formatted_labels):
12751283
if prop == "colors":
12761284
color = self.cmap(self.norm(val))
12771285
elif prop == "sizes":
@@ -1281,10 +1289,7 @@ def legend_elements(self, prop="colors", num="auto",
12811289
h = mlines.Line2D([0], [0], ls="", color=color, ms=size,
12821290
marker=self.get_paths()[0], **kw)
12831291
handles.append(h)
1284-
if hasattr(fmt, "set_locs"):
1285-
fmt.set_locs(label_values)
1286-
l = fmt(lab)
1287-
labels.append(l)
1292+
labels.append(formatted)
12881293

12891294
return handles, labels
12901295

0 commit comments

Comments
 (0)