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

Skip to content

Commit 95524e7

Browse files
authored
Merge pull request #22009 from StefRe/fix/issue21966
FIX: Prevent set_alpha from changing color of legend patch
2 parents 6f2a1d5 + 4695cfe commit 95524e7

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/matplotlib/legend_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,8 @@ def get_first(prop_array):
788788
# Directly set Patch color attributes (must be RGBA tuples).
789789
legend_handle._facecolor = first_color(orig_handle.get_facecolor())
790790
legend_handle._edgecolor = first_color(orig_handle.get_edgecolor())
791+
legend_handle._original_facecolor = orig_handle._original_facecolor
792+
legend_handle._original_edgecolor = orig_handle._original_edgecolor
791793
legend_handle._fill = orig_handle.get_fill()
792794
legend_handle._hatch = orig_handle.get_hatch()
793795
# 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
@@ -879,3 +879,11 @@ def test_subfigure_legend():
879879
ax.plot([0, 1], [0, 1], label="line")
880880
leg = subfig.legend()
881881
assert leg.figure is subfig
882+
883+
884+
def test_setting_alpha_keeps_polycollection_color():
885+
pc = plt.fill_between([0, 1], [2, 3], color='#123456', label='label')
886+
patch = plt.legend().get_patches()[0]
887+
patch.set_alpha(0.5)
888+
assert patch.get_facecolor()[:3] == tuple(pc.get_facecolor()[0][:3])
889+
assert patch.get_edgecolor()[:3] == tuple(pc.get_edgecolor()[0][:3])

0 commit comments

Comments
 (0)