Skip to content

FIX: Prevent set_alpha from changing color of legend patch #22009

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 1 commit into from
Dec 21, 2021
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
2 changes: 2 additions & 0 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,8 @@ def get_first(prop_array):
# Directly set Patch color attributes (must be RGBA tuples).
legend_handle._facecolor = first_color(orig_handle.get_facecolor())
legend_handle._edgecolor = first_color(orig_handle.get_edgecolor())
legend_handle._original_facecolor = orig_handle._original_facecolor
legend_handle._original_edgecolor = orig_handle._original_edgecolor
legend_handle._fill = orig_handle.get_fill()
legend_handle._hatch = orig_handle.get_hatch()
# Hatch color is anomalous in having no getters and setters.
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,3 +879,11 @@ def test_subfigure_legend():
ax.plot([0, 1], [0, 1], label="line")
leg = subfig.legend()
assert leg.figure is subfig


def test_setting_alpha_keeps_polycollection_color():
pc = plt.fill_between([0, 1], [2, 3], color='#123456', label='label')
patch = plt.legend().get_patches()[0]
patch.set_alpha(0.5)
assert patch.get_facecolor()[:3] == tuple(pc.get_facecolor()[0][:3])
assert patch.get_edgecolor()[:3] == tuple(pc.get_edgecolor()[0][:3])