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

Skip to content

Commit 86de3d4

Browse files
committed
Update stem linecolor in legend
1 parent 7947acc commit 86de3d4

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

lib/matplotlib/legend_handler.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,6 @@ def get_ydata(self, legend, xdescent, ydescent, width, height, fontsize):
580580
def create_artists(self, legend, orig_handle,
581581
xdescent, ydescent, width, height, fontsize,
582582
trans):
583-
584583
markerline, stemlines, baseline = orig_handle
585584

586585
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,
@@ -598,9 +597,18 @@ def create_artists(self, legend, orig_handle,
598597
self.update_prop(leg_markerline, markerline, legend)
599598

600599
leg_stemlines = []
600+
601+
# update_prop() usually takes two Line2D collections;
602+
# override temporarily to copy properties from a LineCollection
603+
orig_update_func = self._update_prop_func
604+
self._update_prop_func = self._copy_collection_props
605+
601606
for thisx, thisy in zip(xdata_marker, ydata):
602607
thisline = Line2D([thisx, thisx], [bottom, thisy])
603608
leg_stemlines.append(thisline)
609+
self.update_prop(thisline, stemlines, legend)
610+
# Reset update_prop_func
611+
self._update_prop_func = orig_update_func
604612

605613
leg_baseline = Line2D([np.min(xdata), np.max(xdata)],
606614
[bottom, bottom])
@@ -616,6 +624,13 @@ def create_artists(self, legend, orig_handle,
616624

617625
return artists
618626

627+
def _copy_collection_props(self, legend_handle, orig_handle):
628+
'''
629+
Method to copy properties from a LineCollection (orig_handle) to a
630+
Line2D (legend_handle).
631+
'''
632+
legend_handle._color = orig_handle.get_color()[0]
633+
619634

620635
class HandlerTuple(HandlerBase):
621636
"""
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,7 @@ def test_stem():
28712871
x = np.linspace(0.1, 2 * np.pi, 100)
28722872

28732873
fig, ax = plt.subplots()
2874-
ax.stem(x, np.cos(x), linefmt='C0-', markerfmt='k+', basefmt='C1-.',
2874+
ax.stem(x, np.cos(x), linefmt='C2-', markerfmt='k+', basefmt='C1-.',
28752875
label='Stem')
28762876
ax.legend()
28772877

0 commit comments

Comments
 (0)