Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix legendlabelcolor=‘linecolor’ to handle various corner cases, e.g. step histograms and transparent markers#30328

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
lukashergt wants to merge6 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromlukashergt:fix-legend-labelcolor-linecolor
Open
Show file tree
Hide file tree
Changes fromall 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
22 changes: 17 additions & 5 deletionslib/matplotlib/legend.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -576,7 +576,11 @@ def __init__(
# set the text color

color_getters = { # getter function depends on line or patch
'linecolor': ['get_color', 'get_facecolor'],
'linecolor': ['get_markerfacecolor',
'get_facecolor',
'get_markeredgecolor',
'get_edgecolor',
'get_color'],
'markerfacecolor': ['get_markerfacecolor', 'get_facecolor'],
'mfc': ['get_markerfacecolor', 'get_facecolor'],
'markeredgecolor': ['get_markeredgecolor', 'get_edgecolor'],
Expand All@@ -596,18 +600,26 @@ def __init__(
try:
color = getattr(handle, getter_name)()
if isinstance(color, np.ndarray):
if (
color.shape[0] == 1
or np.isclose(color, color[0]).all()
if color.size == 0:
continue
elif (
color.shape[0] == 1
or np.isclose(color, color[0]).all()
):
text.set_color(color[0])
else:
pass
elif cbook._str_lower_equal(color, 'none'):
continue
elif mpl.colors.to_rgba(color)[3] == 0:
continue
else:
text.set_color(color)
break
except AttributeError:
pass
continue
else:
text.set_color('none')
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly this is executed as a fallback if color could not be detected. Then, it feels wrong to set full transparency, because it makes the text invisible. I think we should rather leave the default as we did before.

Copy link
Author

@lukashergtlukashergtJul 18, 2025
edited
Loading

Choose a reason for hiding this comment

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

Yes, this is a fallback for the case thatfc,ec, andc all havecontinue-ed through, i.e. they are either of the following:

  • an empty array,
  • 'none' or'None',
  • colour with zero alpha value,
  • AttributeError raised on the getter function.

Otherwise they would have broken (note thebreak statement) out of the loop beforehand, in which case theelse case of thefor ... else ... construction won't get invoked.

Final example case from thetest_legend_labelcolor_linecolor_plot test:

importnumpyasnpimportmatplotlib.pyplotaspltx=np.arange(5)fig,ax=plt.subplots()p=ax.plot(x,'o',c='none',mec='none',label="invisible circles with invisible label")leg=ax.legend(labelcolor='linecolor')
image

So yes, the label entry is invisible because the markers and the legend handle are also invisible. In this stand-alone example this seems nonsensical, but something of this sort might come in handy when trying to insert a dummy legend entry in-between other legend entries for spacing.

elif cbook._str_equal(labelcolor, 'none'):
for text in self.texts:
text.set_color(labelcolor)
Expand Down
238 changes: 238 additions & 0 deletionslib/matplotlib/tests/test_legend.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1068,6 +1068,244 @@ def test_legend_labelcolor_rcparam_markerfacecolor_short():
assert mpl.colors.same_color(text.get_color(), color)


def test_legend_labelcolor_linecolor_histograms():
x = np.arange(10)

# testing c kwarg for bar, step, and stepfilled histograms
fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='bar', color='r',
label="red bar hist with a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='step', color='g',
label="green step hist with a green label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'g')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_edgecolor())
assert mpl.colors.same_color(tc, h[0].get_edgecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='stepfilled', color='b',
label="blue stepfilled hist with a blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())

# testing c, fc, and ec combinations for bar histograms
fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='bar', color='r', ec='b',
label="red bar hist with blue edges and a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='bar', fc='r', ec='b',
label="red bar hist with blue edges and a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='bar', fc='none', ec='b',
label="unfilled blue bar hist with a blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_edgecolor())
assert mpl.colors.same_color(tc, h[0].get_edgecolor())

# testing c, and ec combinations for step histograms
fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='step', color='r', ec='b',
label="blue step hist with a blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_edgecolor())
assert mpl.colors.same_color(tc, h[0].get_edgecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='step', ec='b',
label="blue step hist with a blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_edgecolor())
assert mpl.colors.same_color(tc, h[0].get_edgecolor())

# testing c, fc, and ec combinations for stepfilled histograms
fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='stepfilled', color='r', ec='b',
label="red stepfilled hist, blue edges, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='stepfilled', fc='r', ec='b',
label="red stepfilled hist, blue edges, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='stepfilled', fc='none', ec='b',
label="unfilled blue stepfilled hist, blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_edgecolor())
assert mpl.colors.same_color(tc, h[0].get_edgecolor())

fig, ax = plt.subplots()
_, _, h = ax.hist(x, histtype='stepfilled', fc='r', ec='none',
label="edgeless red stepfilled hist with a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_patches()[0].get_facecolor())
assert mpl.colors.same_color(tc, h[0].get_facecolor())
plt.close('all')


def test_legend_labelcolor_linecolor_plot():
x = np.arange(5)

# testing line plot
fig, ax = plt.subplots()
p = ax.plot(x, c='r', label="red line with a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_color())
assert mpl.colors.same_color(tc, p[0].get_color())

# testing c, fc, and ec combinations for maker plots
fig, ax = plt.subplots()
p = ax.plot(x, 'o', c='r', label="red circles with a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_color())
assert mpl.colors.same_color(tc, p[0].get_color())

fig, ax = plt.subplots()
p = ax.plot(x, 'o', c='r', mec='b', label="red circles, blue edges, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_color())
assert mpl.colors.same_color(tc, p[0].get_color())

fig, ax = plt.subplots()
p = ax.plot(x, 'o', mfc='r', mec='b', label="red circles, blue edges, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_markerfacecolor())
assert mpl.colors.same_color(tc, p[0].get_markerfacecolor())

# 'none' cases
fig, ax = plt.subplots()
p = ax.plot(x, 'o', mfc='none', mec='b', label="blue unfilled circles, blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_markeredgecolor())
assert mpl.colors.same_color(tc, p[0].get_markeredgecolor())

fig, ax = plt.subplots()
p = ax.plot(x, 'o', mfc='r', mec='none', label="red edgeless circles, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_markerfacecolor())
assert mpl.colors.same_color(tc, p[0].get_markerfacecolor())

fig, ax = plt.subplots()
p = ax.plot(x, 'o', c='none', mec='none', label="invisible circles and label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'none')
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_markerfacecolor())
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_markeredgecolor())
assert mpl.colors.same_color(tc, leg.get_lines()[0].get_color())
assert mpl.colors.same_color(tc, p[0].get_markerfacecolor())
assert mpl.colors.same_color(tc, p[0].get_markeredgecolor())
assert mpl.colors.same_color(tc, p[0].get_color())
plt.close('all')


def test_legend_labelcolor_linecolor_scatter():
x = np.arange(5)

# testing c, fc, and ec combinations for scatter plots
fig, ax = plt.subplots()
p = ax.scatter(x, x, c='r', label="red circles with a red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.legend_handles[0].get_facecolor())
assert mpl.colors.same_color(tc, p.get_facecolor())

fig, ax = plt.subplots()
p = ax.scatter(x, x, c='r', ec='b', label="red circles, blue edges, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.legend_handles[0].get_facecolor())
assert mpl.colors.same_color(tc, p.get_facecolor())

fig, ax = plt.subplots()
p = ax.scatter(x, x, fc='r', ec='b', label="red circles, blue edges, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.legend_handles[0].get_facecolor())
assert mpl.colors.same_color(tc, p.get_facecolor())

# 'none' cases
fig, ax = plt.subplots()
p = ax.scatter(x, x, fc='none', ec='b', label="blue unfilled circles, blue label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'b')
assert mpl.colors.same_color(tc, leg.legend_handles[0].get_edgecolor())
assert mpl.colors.same_color(tc, p.get_edgecolor())

fig, ax = plt.subplots()
p = ax.scatter(x, x, fc='r', ec='none', label="red edgeless circles, red label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'r')
assert mpl.colors.same_color(tc, leg.legend_handles[0].get_facecolor())
assert mpl.colors.same_color(tc, p.get_facecolor())

fig, ax = plt.subplots()
p = ax.scatter(x, x, c='none', ec='none', label="invisible circles and label")
leg = ax.legend(labelcolor='linecolor')
tc = leg.texts[0].get_color()
assert mpl.colors.same_color(tc, 'none')
plt.close('all')


@pytest.mark.filterwarnings("ignore:No artists with labels found to put in legend")
def test_get_set_draggable():
legend = plt.legend()
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp