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

Skip to content

Commit 16d88d8

Browse files
committed
Update stem linecolor in legend
1 parent caf7736 commit 16d88d8

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
@@ -589,7 +589,6 @@ def get_ydata(self, legend, xdescent, ydescent, width, height, fontsize):
589589
def create_artists(self, legend, orig_handle,
590590
xdescent, ydescent, width, height, fontsize,
591591
trans):
592-
593592
markerline, stemlines, baseline = orig_handle
594593

595594
xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,
@@ -607,9 +606,18 @@ def create_artists(self, legend, orig_handle,
607606
self.update_prop(leg_markerline, markerline, legend)
608607

609608
leg_stemlines = []
609+
610+
# update_prop() usually takes two Line2D collections;
611+
# override temporarily to copy properties from a LineCollection
612+
orig_update_func = self._update_prop_func
613+
self._update_prop_func = self._copy_collection_props
614+
610615
for thisx, thisy in zip(xdata_marker, ydata):
611616
thisline = Line2D([thisx, thisx], [bottom, thisy])
612617
leg_stemlines.append(thisline)
618+
self.update_prop(thisline, stemlines, legend)
619+
# Reset update_prop_func
620+
self._update_prop_func = orig_update_func
613621

614622
leg_baseline = Line2D([np.min(xdata), np.max(xdata)],
615623
[bottom, bottom])
@@ -625,6 +633,13 @@ def create_artists(self, legend, orig_handle,
625633

626634
return artists
627635

636+
def _copy_collection_props(self, legend_handle, orig_handle):
637+
'''
638+
Method to copy properties from a LineCollection (orig_handle) to a
639+
Line2D (legend_handle).
640+
'''
641+
legend_handle._color = orig_handle.get_color()[0]
642+
628643

629644
class HandlerTuple(HandlerBase):
630645
"""
Loading

lib/matplotlib/tests/test_axes.py

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

29302930
fig, ax = plt.subplots()
2931-
ax.stem(x, np.cos(x), linefmt='C0-', markerfmt='k+', basefmt='C1-.',
2931+
ax.stem(x, np.cos(x), linefmt='C2-', markerfmt='k+', basefmt='C1-.',
29322932
label='Stem')
29332933
ax.legend()
29342934

0 commit comments

Comments
 (0)