Skip to content

Commit 8998cbc

Browse files
committed
Add textcolor to legend based on labelcolor string
As raised by matplotlib#20577, setting `labelcolor` to any of the string options did not work with a scatter plot as it is a PathCollection with possible multiple color values. This commit fixes that. Now, if there is a Colormap or a scatter plot with multiple values, then, the legend text color is not changed, but otherwise, the text color is changed to the color of the scatter markers.
1 parent 884397a commit 8998cbc

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/matplotlib/legend.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,24 @@ def val_or_rc(val, rc_name):
562562
if isinstance(labelcolor, str) and labelcolor in color_getters:
563563
getter_names = color_getters[labelcolor]
564564
for handle, text in zip(self.legendHandles, self.texts):
565+
try:
566+
if isinstance(handle.cmap, colors.LinearSegmentedColormap):
567+
continue
568+
except AttributeError:
569+
pass
565570
for getter_name in getter_names:
566571
try:
567572
color = getattr(handle, getter_name)()
568-
text.set_color(color)
573+
if isinstance(color, np.ndarray):
574+
if (
575+
color.shape[0] == 1
576+
or np.isclose(color, color[0]).all()
577+
):
578+
text.set_color(color[0])
579+
else:
580+
pass
581+
else:
582+
text.set_color(color)
569583
break
570584
except AttributeError:
571585
pass

0 commit comments

Comments
 (0)