Skip to content

Add textcolor to legend based on labelcolor string #24122

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
merged 7 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 15 additions & 1 deletion lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,24 @@ def val_or_rc(val, rc_name):
if isinstance(labelcolor, str) and labelcolor in color_getters:
getter_names = color_getters[labelcolor]
for handle, text in zip(self.legendHandles, self.texts):
try:
if handle.get_array() is not None:
continue
except AttributeError:
pass
for getter_name in getter_names:
try:
color = getattr(handle, getter_name)()
text.set_color(color)
if isinstance(color, np.ndarray):
if (
color.shape[0] == 1
or np.isclose(color, color[0]).all()
):
text.set_color(color[0])
else:
pass
else:
text.set_color(color)
break
except AttributeError:
pass
Expand Down
120 changes: 120 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,41 @@ def test_legend_labelcolor_linecolor():
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_linecolor():
# test the labelcolor for labelcolor='linecolor' on PathCollection
fig, ax = plt.subplots()
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', c='r')
ax.scatter(np.arange(10), np.arange(10)*2, label='#2', c='g')
ax.scatter(np.arange(10), np.arange(10)*3, label='#3', c='b')

leg = ax.legend(labelcolor='linecolor')
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_linecolor_iterable():
# test the labelcolor for labelcolor='linecolor' on PathCollection
# with iterable colors
fig, ax = plt.subplots()
colors = np.random.default_rng().choice(['r', 'g', 'b'], 10)
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', c=colors)

leg = ax.legend(labelcolor='linecolor')
text, = leg.get_texts()
assert mpl.colors.same_color(text.get_color(), 'black')


def test_legend_pathcollection_labelcolor_linecolor_cmap():
# test the labelcolor for labelcolor='linecolor' on PathCollection
# with a colormap
fig, ax = plt.subplots()
ax.scatter(np.arange(10), np.arange(10), c=np.arange(10), label='#1')

leg = ax.legend(labelcolor='linecolor')
text, = leg.get_texts()
assert mpl.colors.same_color(text.get_color(), 'black')


def test_legend_labelcolor_markeredgecolor():
# test the labelcolor for labelcolor='markeredgecolor'
fig, ax = plt.subplots()
Expand All @@ -683,6 +718,49 @@ def test_legend_labelcolor_markeredgecolor():
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_markeredgecolor():
# test the labelcolor for labelcolor='markeredgecolor' on PathCollection
fig, ax = plt.subplots()
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', edgecolor='r')
ax.scatter(np.arange(10), np.arange(10)*2, label='#2', edgecolor='g')
ax.scatter(np.arange(10), np.arange(10)*3, label='#3', edgecolor='b')

leg = ax.legend(labelcolor='markeredgecolor')
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_markeredgecolor_iterable():
# test the labelcolor for labelcolor='markeredgecolor' on PathCollection
# with iterable colors
fig, ax = plt.subplots()
colors = np.random.default_rng().choice(['r', 'g', 'b'], 10)
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', edgecolor=colors)

leg = ax.legend(labelcolor='markeredgecolor')
for text, color in zip(leg.get_texts(), ['k']):
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_markeredgecolor_cmap():
# test the labelcolor for labelcolor='markeredgecolor' on PathCollection
# with a colormap
fig, ax = plt.subplots()
edgecolors = mpl.cm.viridis(np.random.rand(10))
ax.scatter(
np.arange(10),
np.arange(10),
label='#1',
c=np.arange(10),
edgecolor=edgecolors,
cmap="Reds"
)

leg = ax.legend(labelcolor='markeredgecolor')
for text, color in zip(leg.get_texts(), ['k']):
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_labelcolor_markerfacecolor():
# test the labelcolor for labelcolor='markerfacecolor'
fig, ax = plt.subplots()
Expand All @@ -695,6 +773,48 @@ def test_legend_labelcolor_markerfacecolor():
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_markerfacecolor():
# test the labelcolor for labelcolor='markerfacecolor' on PathCollection
fig, ax = plt.subplots()
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', facecolor='r')
ax.scatter(np.arange(10), np.arange(10)*2, label='#2', facecolor='g')
ax.scatter(np.arange(10), np.arange(10)*3, label='#3', facecolor='b')

leg = ax.legend(labelcolor='markerfacecolor')
for text, color in zip(leg.get_texts(), ['r', 'g', 'b']):
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_markerfacecolor_iterable():
# test the labelcolor for labelcolor='markerfacecolor' on PathCollection
# with iterable colors
fig, ax = plt.subplots()
colors = np.random.default_rng().choice(['r', 'g', 'b'], 10)
ax.scatter(np.arange(10), np.arange(10)*1, label='#1', facecolor=colors)

leg = ax.legend(labelcolor='markerfacecolor')
for text, color in zip(leg.get_texts(), ['k']):
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_pathcollection_labelcolor_markfacecolor_cmap():
# test the labelcolor for labelcolor='markerfacecolor' on PathCollection
# with colormaps
fig, ax = plt.subplots()
facecolors = mpl.cm.viridis(np.random.rand(10))
ax.scatter(
np.arange(10),
np.arange(10),
label='#1',
c=np.arange(10),
facecolor=facecolors
)

leg = ax.legend(labelcolor='markerfacecolor')
for text, color in zip(leg.get_texts(), ['k']):
assert mpl.colors.same_color(text.get_color(), color)


@pytest.mark.parametrize('color', ('red', 'none', (.5, .5, .5)))
def test_legend_labelcolor_rcparam_single(color):
# test the rcParams legend.labelcolor for a single color
Expand Down