Skip to content

#5856: added option to create vertically-oriented stem plots #6168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
10 changes: 10 additions & 0 deletions doc/users/whats_new/stem_orientation.rst
Original file line number Diff line number Diff line change
@@ -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')
66 changes: 55 additions & 11 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not conform with numpy doc specs see #6240 for a similar fix.
If you change it to Other Parameters it should work. As it is this section will not be in the web generated version of the docs.

See https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt for more numpy docs info.

The output from the docs build should this is in https://travis-ci.org/matplotlib/matplotlib/jobs/118347853#L1036

--------------------
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 <http://www.mathworks.com/help/techdoc/ref/stem.html>`_
for details.


**Example:**

Examples
--------
.. plot:: mpl_examples/pylab_examples/stem_plot.py
"""
remember_hold = self._hold
Expand Down Expand Up @@ -2420,22 +2444,42 @@ 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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably do some validation here to make sure 'aardvark' or the like does not get passed in.


if orientation not in ('horizontal', 'vertical'):
raise ValueError("'%s' is not a valid orientation" % orientation)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bigger change, but could you refactor this to have on if block up here which does the flipping (and maybe rename x, y?) and then the rest of the logic is the same.

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:
bottom = 0

stemlines = []
for thisx, thisy in zip(x, y):
l, = self.plot([thisx, thisx], [bottom, thisy], linefmt,
label="_nolegend_")
if orientation == 'vertical':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this if block be eliminated?

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)

Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading