diff --git a/doc/users/whats_new/stem_orientation.rst b/doc/users/whats_new/stem_orientation.rst new file mode 100644 index 000000000000..3495333aa109 --- /dev/null +++ b/doc/users/whats_new/stem_orientation.rst @@ -0,0 +1,10 @@ +Added ``orientation`` parameter for stem plots +----------------------------------- +When creating stem plots, you can now pass in an ``orientation`` argument to :func:`stem`. + +Currently, only ``vertical`` and ``horizontal`` orientations are supported, +with ``horizontal`` being the default. + +Example +``````` +stem(x, x, orientation='vertical') diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 49d755ae1954..a2c1c1c270a1 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2364,8 +2364,10 @@ def stem(self, *args, **kwargs): Call signatures:: - stem(y, linefmt='b-', markerfmt='bo', basefmt='r-') - stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-') + stem(y, linefmt='b-', markerfmt='bo', basefmt='r-', + orientation = {'horizontal'|'vertical'}) + stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-', + orientation = {'horizontal'|'vertical'}) A stem plot plots vertical lines (using *linefmt*) at each *x* location from the baseline to *y*, and places a marker there @@ -2374,17 +2376,39 @@ def stem(self, *args, **kwargs): If no *x* values are provided, the default is (0, 1, ..., len(y) - 1) - Return value is a tuple (*markerline*, *stemlines*, - *baseline*). + Optional parameters: + -------------------- + linefmt : line format string + Format for the lines protruding from the baseline to the + stem markers. + + markerfmt: marker format string + Format for the markers. + + basefmt: line format string + Format for the baseline. + + vertical: string, optional (horizontal) + If 'vertical', will produce a vertically-oriented stem plot, + else it will produce a horizontally-oriented stem plot. + + bottom: num, optional (0) + The location of the base line. + + label: string + Label for the stem container returned. + + Returns + ------- + The return value is ``(markerline, stemlines, baseline)``. .. seealso:: This `document `_ for details. - - **Example:** - + Examples + -------- .. plot:: mpl_examples/pylab_examples/stem_plot.py """ remember_hold = self._hold @@ -2420,9 +2444,19 @@ def stem(self, *args, **kwargs): except IndexError: basefmt = kwargs.pop('basefmt', 'r-') + # Check the orientation variable to see if the user + # wants a vertical or horizontal stem plot + orientation = kwargs.pop('orientation', 'horizontal') + + if orientation not in ('horizontal', 'vertical'): + raise ValueError("'%s' is not a valid orientation" % orientation) + bottom = kwargs.pop('bottom', None) label = kwargs.pop('label', None) + if orientation == 'vertical': + x, y = y, x + markerline, = self.plot(x, y, markerfmt, label="_nolegend_") if bottom is None: @@ -2430,12 +2464,22 @@ def stem(self, *args, **kwargs): stemlines = [] for thisx, thisy in zip(x, y): - l, = self.plot([thisx, thisx], [bottom, thisy], linefmt, - label="_nolegend_") + if orientation == 'vertical': + thisx, thisy = thisy, thisx + l, = self.plot([bottom, thisy], [thisx, thisx], linefmt, + label="_nolegend_") + else: + l, = self.plot([thisx, thisx], [bottom, thisy], linefmt, + label="_nolegend_") stemlines.append(l) - baseline, = self.plot([np.amin(x), np.amax(x)], [bottom, bottom], - basefmt, label="_nolegend_") + if orientation == 'vertical': + x, y = y, x + baseline, = self.plot([bottom, bottom], [np.amin(x), np.amax(x)], + basefmt, label="_nolegend_") + else: + baseline, = self.plot([np.amin(x), np.amax(x)], [bottom, bottom], + basefmt, label="_nolegend_") self.hold(remember_hold) diff --git a/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.pdf b/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.pdf new file mode 100644 index 000000000000..88330dfed96d Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.pdf differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.png b/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.png new file mode 100644 index 000000000000..515b336f1dc0 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.png differ diff --git a/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.svg b/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.svg new file mode 100644 index 000000000000..34ba521a909f --- /dev/null +++ b/lib/matplotlib/tests/baseline_images/test_axes/stem_orientation.svg @@ -0,0 +1,598 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 379a62405597..48cebf8a5ac4 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -2348,6 +2348,10 @@ def test_stem_dates(): ax.stem([x, x1], [y, y1], "*-") +@image_comparison(baseline_images=['stem_orientation']) +def test_stem_orientation(): + x = np.linspace(0.1, 2*np.pi, 10) + plt.stem(x, np.cos(x), orientation='vertical') @image_comparison(baseline_images=['hist_stacked_stepfilled_alpha']) def test_hist_stacked_stepfilled_alpha():