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

Appearance settings

Commit dcfc7a1

Browse filesBrowse files
authored
Merge pull request #20267 from meeseeksmachine/auto-backport-of-pr-20265-on-v3.4.x
Backport PR #20265 on branch v3.4.x (Legend edgecolor face)
2 parents b25263e + 88da530 commit dcfc7a1
Copy full SHA for dcfc7a1

File tree

Expand file treeCollapse file tree

2 files changed

+22
-17
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+22
-17
lines changed

‎lib/matplotlib/legend_handler.py

Copy file name to clipboardExpand all lines: lib/matplotlib/legend_handler.py
+14-17Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def legend_artist(self, legend, orig_handle, fontsize, handlebox)
3131
from matplotlib.lines import Line2D
3232
from matplotlib.patches import Rectangle
3333
import matplotlib.collections as mcoll
34-
import matplotlib.colors as mcolors
3534

3635

3736
def update_from_first_child(tgt, src):
@@ -734,32 +733,30 @@ class HandlerPolyCollection(HandlerBase):
734733
"""
735734
def _update_prop(self, legend_handle, orig_handle):
736735
def first_color(colors):
737-
if colors is None:
738-
return None
739-
colors = mcolors.to_rgba_array(colors)
740-
if len(colors):
741-
return colors[0]
742-
else:
743-
return "none"
736+
if colors.size == 0:
737+
return (0, 0, 0, 0)
738+
return tuple(colors[0])
744739

745740
def get_first(prop_array):
746741
if len(prop_array):
747742
return prop_array[0]
748743
else:
749744
return None
750-
edgecolor = getattr(orig_handle, '_original_edgecolor',
751-
orig_handle.get_edgecolor())
752-
legend_handle.set_edgecolor(first_color(edgecolor))
753-
facecolor = getattr(orig_handle, '_original_facecolor',
754-
orig_handle.get_facecolor())
755-
legend_handle.set_facecolor(first_color(facecolor))
756-
legend_handle.set_fill(orig_handle.get_fill())
757-
legend_handle.set_hatch(orig_handle.get_hatch())
745+
746+
# orig_handle is a PolyCollection and legend_handle is a Patch.
747+
# Directly set Patch color attributes (must be RGBA tuples).
748+
legend_handle._facecolor = first_color(orig_handle.get_facecolor())
749+
legend_handle._edgecolor = first_color(orig_handle.get_edgecolor())
750+
legend_handle._fill = orig_handle.get_fill()
751+
legend_handle._hatch = orig_handle.get_hatch()
752+
# Hatch color is anomalous in having no getters and setters.
753+
legend_handle._hatch_color = orig_handle._hatch_color
754+
# Setters are fine for the remaining attributes.
758755
legend_handle.set_linewidth(get_first(orig_handle.get_linewidths()))
759756
legend_handle.set_linestyle(get_first(orig_handle.get_linestyles()))
760757
legend_handle.set_transform(get_first(orig_handle.get_transforms()))
761758
legend_handle.set_figure(orig_handle.get_figure())
762-
legend_handle.set_alpha(orig_handle.get_alpha())
759+
# Alpha is already taken into account by the color attributes.
763760

764761
def create_artists(self, legend, orig_handle,
765762
xdescent, ydescent, width, height, fontsize, trans):

‎lib/matplotlib/tests/test_legend.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_legend.py
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,3 +734,11 @@ def test_plot_multiple_label_incorrect_length_exception():
734734
label = ['high', 'low', 'medium']
735735
fig, ax = plt.subplots()
736736
ax.plot(x, y, label=label)
737+
738+
739+
def test_legend_face_edgecolor():
740+
# Smoke test for PolyCollection legend handler with 'face' edgecolor.
741+
fig, ax = plt.subplots()
742+
ax.fill_between([0, 1, 2], [1, 2, 3], [2, 3, 4],
743+
facecolor='r', edgecolor='face', label='Fill')
744+
ax.legend()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.