Skip to content

Commit 7f10300

Browse files
vecuencaQuLogic
authored andcommitted
Add option to create vertically-oriented stem plots
* Added docstrings for optional args in stem, made vertical default to false * Added flipping and renamed xs and ys in stem
1 parent 0375315 commit 7f10300

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ def broken_barh(self, xranges, yrange, **kwargs):
27232723

27242724
@_preprocess_data()
27252725
def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
2726-
label=None, use_line_collection=True):
2726+
label=None, use_line_collection=True, vertical=False):
27272727
"""
27282728
Create a stem plot.
27292729
@@ -2774,6 +2774,9 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
27742774
basefmt : str, default: 'C3-' ('C2-' in classic mode)
27752775
A format string defining the properties of the baseline.
27762776
2777+
vertical : bool, optional (False)
2778+
If 'True', will produce a vertically-oriented stem plot.
2779+
27772780
bottom : float, default: 0
27782781
The y-position of the baseline.
27792782
@@ -2862,9 +2865,16 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28622865
else:
28632866
basestyle, basemarker, basecolor = _process_plot_format(basefmt)
28642867

2868+
# Check if the user wants a vertical stem plot
2869+
if vertical:
2870+
x, y = y, x
2871+
28652872
# New behaviour in 3.1 is to use a LineCollection for the stemlines
28662873
if use_line_collection:
2867-
stemlines = [((xi, bottom), (xi, yi)) for xi, yi in zip(x, y)]
2874+
if vertical:
2875+
stemlines = [((bottom, yi), (xi, yi)) for xi, yi in zip(x, y)]
2876+
else:
2877+
stemlines = [((xi, bottom), (xi, yi)) for xi, yi in zip(x, y)]
28682878
if linestyle is None:
28692879
linestyle = rcParams['lines.linestyle']
28702880
stemlines = mcoll.LineCollection(stemlines, linestyles=linestyle,
@@ -2875,17 +2885,27 @@ def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
28752885
else:
28762886
stemlines = []
28772887
for xi, yi in zip(x, y):
2878-
l, = self.plot([xi, xi], [bottom, yi],
2888+
if vertical:
2889+
xs = [bottom, xi]
2890+
ys = [yi, yi]
2891+
else:
2892+
xs = [xi, xi]
2893+
ys = [bottom, yi]
2894+
l, = self.plot(xs, ys,
28792895
color=linecolor, linestyle=linestyle,
28802896
marker=linemarker, label="_nolegend_")
28812897
stemlines.append(l)
28822898

28832899
markerline, = self.plot(x, y, color=markercolor, linestyle=markerstyle,
28842900
marker=markermarker, label="_nolegend_")
2885-
2886-
baseline, = self.plot([np.min(x), np.max(x)], [bottom, bottom],
2887-
color=basecolor, linestyle=basestyle,
2888-
marker=basemarker, label="_nolegend_")
2901+
if vertical:
2902+
baseline, = self.plot([bottom, bottom], [np.min(x), np.max(x)],
2903+
color=basecolor, linestyle=basestyle,
2904+
marker=basemarker, label="_nolegend_")
2905+
else:
2906+
baseline, = self.plot([np.min(x), np.max(x)], [bottom, bottom],
2907+
color=basecolor, linestyle=basestyle,
2908+
marker=basemarker, label="_nolegend_")
28892909

28902910
stem_container = StemContainer((markerline, stemlines, baseline),
28912911
label=label)

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,11 +3018,12 @@ def stackplot(
30183018
@_copy_docstring_and_deprecators(Axes.stem)
30193019
def stem(
30203020
*args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,
3021-
label=None, use_line_collection=True, data=None):
3021+
label=None, use_line_collection=True, vertical=False,
3022+
data=None):
30223023
return gca().stem(
30233024
*args, linefmt=linefmt, markerfmt=markerfmt, basefmt=basefmt,
30243025
bottom=bottom, label=label,
3025-
use_line_collection=use_line_collection,
3026+
use_line_collection=use_line_collection, vertical=vertical,
30263027
**({"data": data} if data is not None else {}))
30273028

30283029

0 commit comments

Comments
 (0)