Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4630a4a

Browse files
QuLogicmeeseeksmachine
authored andcommitted
Backport PR #22009: FIX: Prevent set_alpha from changing color of legend patch
1 parent 74d7afb commit 4630a4a

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

lib/matplotlib/legend_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,8 @@ def get_first(prop_array):
815815
# Directly set Patch color attributes (must be RGBA tuples).
816816
legend_handle._facecolor = first_color(orig_handle.get_facecolor())
817817
legend_handle._edgecolor = first_color(orig_handle.get_edgecolor())
818+
legend_handle._original_facecolor = orig_handle._original_facecolor
819+
legend_handle._original_edgecolor = orig_handle._original_edgecolor
818820
legend_handle._fill = orig_handle.get_fill()
819821
legend_handle._hatch = orig_handle.get_hatch()
820822
# Hatch color is anomalous in having no getters and setters.

lib/matplotlib/tests/test_legend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,11 @@ def test_subfigure_legend():
880880
ax.plot([0, 1], [0, 1], label="line")
881881
leg = subfig.legend()
882882
assert leg.figure is subfig
883+
884+
885+
def test_setting_alpha_keeps_polycollection_color():
886+
pc = plt.fill_between([0, 1], [2, 3], color='#123456', label='label')
887+
patch = plt.legend().get_patches()[0]
888+
patch.set_alpha(0.5)
889+
assert patch.get_facecolor()[:3] == tuple(pc.get_facecolor()[0][:3])
890+
assert patch.get_edgecolor()[:3] == tuple(pc.get_edgecolor()[0][:3])

0 commit comments

Comments
 (0)