Skip to content

Commit 4cd53fe

Browse files
committed
Rename stem parameters to be clearer.
They are no longer x/y when using a different orientation. Also, this should fix the unit conversion to use the correct Acis.
1 parent 5e35943 commit 4cd53fe

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,23 +2727,28 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
27272727
"""
27282728
Create a stem plot.
27292729
2730-
A stem plot plots vertical lines at each *x* location from the baseline
2731-
to *y*, and places a marker there.
2730+
A stem plot draws lines perpendicular to a baseline at each location
2731+
*locs* from the baseline to *heads*, and places a marker there. For
2732+
horizontal stem plots (the default), the *locs* are *x* positions, and
2733+
the *heads* are *y* values. For vertical stem plots, the *locs* are
2734+
*y* positions, and the *heads* are *x* values.
27322735
27332736
Call signature::
27342737
2735-
stem([x,] y, linefmt=None, markerfmt=None, basefmt=None)
2738+
stem([locs,] heads, linefmt=None, markerfmt=None, basefmt=None)
27362739
2737-
The x-positions are optional. The formats may be provided either as
2738-
positional or as keyword-arguments.
2740+
The *locs*-positions are optional. The formats may be provided either
2741+
as positional or as keyword-arguments.
27392742
27402743
Parameters
27412744
----------
2742-
x : array-like, optional
2743-
The x-positions of the stems. Default: (0, 1, ..., len(y) - 1).
2745+
locs : array-like, default: (0, 1, ..., len(heads) - 1)
2746+
For horizontal stem plots, the x-positions of the stems.
2747+
For vertical stem plots, the y-positions of the stems.
27442748
2745-
y : array-like
2746-
The y-values of the stem heads.
2749+
heads : array-like
2750+
For horizontal stem plots, the y-values of the stem heads.
2751+
For vertical stem plots, the x-values of the stem heads.
27472752
27482753
linefmt : str, optional
27492754
A string defining the properties of the vertical lines. Usually,
@@ -2774,7 +2779,7 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
27742779
basefmt : str, default: 'C3-' ('C2-' in classic mode)
27752780
A format string defining the properties of the baseline.
27762781
2777-
orientation : string, optional (horizontal)
2782+
orientation : str, default: 'horizontal'
27782783
If 'vertical', will produce a vertically-oriented stem plot,
27792784
else it will produce a horizontally-oriented stem plot.
27802785
@@ -2811,15 +2816,20 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28112816
orientation=orientation)
28122817

28132818
if len(args) == 1:
2814-
y, = args
2815-
x = np.arange(len(y))
2819+
heads, = args
2820+
locs = np.arange(len(heads))
28162821
args = ()
28172822
else:
2818-
x, y, *args = args
2823+
locs, heads, *args = args
28192824

2820-
self._process_unit_info(xdata=x, ydata=y)
2821-
x = self.convert_xunits(x)
2822-
y = self.convert_yunits(y)
2825+
if orientation == 'horizontal':
2826+
self._process_unit_info(xdata=locs, ydata=heads)
2827+
locs = self.convert_xunits(locs)
2828+
heads = self.convert_yunits(heads)
2829+
else:
2830+
self._process_unit_info(xdata=heads, ydata=locs)
2831+
heads = self.convert_xunits(heads)
2832+
locs = self.convert_yunits(locs)
28232833

28242834
# defaults for formats
28252835
if linefmt is None:
@@ -2868,16 +2878,14 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28682878
else:
28692879
basestyle, basemarker, basecolor = _process_plot_format(basefmt)
28702880

2871-
# Check if the user wants a vertical stem plot
2872-
if orientation == 'vertical':
2873-
x, y = y, x
2874-
28752881
# New behaviour in 3.1 is to use a LineCollection for the stemlines
28762882
if use_line_collection:
28772883
if orientation == 'vertical':
2878-
stemlines = [((bottom, yi), (xi, yi)) for xi, yi in zip(x, y)]
2884+
stemlines = [
2885+
((bottom, li), (hi, li)) for li, hi in zip(locs, heads)]
28792886
else:
2880-
stemlines = [((xi, bottom), (xi, yi)) for xi, yi in zip(x, y)]
2887+
stemlines = [
2888+
((li, bottom), (li, hi)) for li, hi in zip(locs, heads)]
28812889
if linestyle is None:
28822890
linestyle = rcParams['lines.linestyle']
28832891
stemlines = mcoll.LineCollection(stemlines, linestyles=linestyle,
@@ -2887,28 +2895,35 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28872895
# Old behaviour is to plot each of the lines individually
28882896
else:
28892897
stemlines = []
2890-
for xi, yi in zip(x, y):
2898+
for li, hi in zip(locs, heads):
28912899
if orientation == 'vertical':
2892-
xs = [bottom, xi]
2893-
ys = [yi, yi]
2900+
xs = [bottom, hi]
2901+
ys = [li, li]
28942902
else:
2895-
xs = [xi, xi]
2896-
ys = [bottom, yi]
2903+
xs = [li, li]
2904+
ys = [bottom, hi]
28972905
l, = self.plot(xs, ys,
28982906
color=linecolor, linestyle=linestyle,
28992907
marker=linemarker, label="_nolegend_")
29002908
stemlines.append(l)
29012909

2902-
markerline, = self.plot(x, y, color=markercolor, linestyle=markerstyle,
2903-
marker=markermarker, label="_nolegend_")
2910+
if orientation == 'vertical':
2911+
markerline, = self.plot(heads, locs, color=markercolor,
2912+
linestyle=markerstyle, marker=markermarker,
2913+
label="_nolegend_")
2914+
else:
2915+
markerline, = self.plot(locs, heads, color=markercolor,
2916+
linestyle=markerstyle, marker=markermarker,
2917+
label="_nolegend_")
29042918

29052919
if orientation == 'vertical':
2906-
x, y = y, x
2907-
baseline, = self.plot([bottom, bottom], [np.min(x), np.max(x)],
2920+
baseline, = self.plot([bottom, bottom],
2921+
[np.min(locs), np.max(locs)],
29082922
color=basecolor, linestyle=basestyle,
29092923
marker=basemarker, label="_nolegend_")
29102924
else:
2911-
baseline, = self.plot([np.min(x), np.max(x)], [bottom, bottom],
2925+
baseline, = self.plot([np.min(locs), np.max(locs)],
2926+
[bottom, bottom],
29122927
color=basecolor, linestyle=basestyle,
29132928
marker=basemarker, label="_nolegend_")
29142929

0 commit comments

Comments
 (0)