-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix legend labelcolor=‘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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dabaf62
335e76e
8d9555e
0fc6fbe
db46a5c
64e3a86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'], | ||
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is a fallback for the case that
Otherwise they would have broken (note the Final example case from the import numpy as np
import matplotlib.pyplot as plt
x = 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') ![]() 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) | ||
|
Uh oh!
There was an error while loading. Please reload this page.