8000 Make Axes.stem take at least one argument. by dmcdougall · Pull Request #1139 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Make Axes.stem take at least one argument. #1139

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

Merged
merged 5 commits into from
Jan 4, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make Axes.stem take at least one argument.
When it takes only one, the abscissae default to np.arange(len(y))
  • Loading branch information
dmcdougall committed Nov 13, 2012
commit eb953ba8f913c79e2cf5286ef4be4e2f1ae6ecea
27 changes: 24 additions & 3 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5074,20 +5074,22 @@ def broken_barh(self, xranges, yrange, **kwargs):

return col

def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-',
bottom=None, label=None):
def stem(self, *args, **kwargs):
"""
Create a stem plot.

Call signature::
Call signatures::

stem(y, linefmt='b-', markerfmt='bo', basefmt='r-')
stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')

A stem plot plots vertical lines (using *linefmt*) at each *x*
location from the baseline to *y*, and places a marker there
using *markerfmt*. A horizontal line at 0 is is plotted using
*basefmt*.

If no *x* values are provided, the default is (0, 1, ..., len(y) - 1)

Return value is a tuple (*markerline*, *stemlines*,
*baseline*).

Expand All @@ -5104,6 +5106,25 @@ def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-',
if not self._hold: self.cla()
self.hold(True)

# Assume there's at least one data array
y = np.asarray(args[0], dtype=np.float)

# Try a second one
try:
second = np.asarray(args[1], dtype=np.float)
x, y = y, second
except:
Copy link
Member

Choose a reason for hiding this comment

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

At the very least, catch Exception, but I would rather catch IndexError and whatever else might come from numpy (ValueError, maybe?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I wanted to catch both so didn't specify -- perhaps catching Exception also catches some magical python ponies that I am unaware of?

Copy link
Member Author

Choose a reason for hiding this comment

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

The IndexError comes from args[1] being out of bounds. The ValueError comes from the casting to np.float being invalid. This is the case in your example of stem(x, 'r--').

Copy link
Member

Choose a reason for hiding this comment

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

If you want to catch both, then do except (IndexError, ValueError):. There is a difference between except: and except Exception:. See here: http://docs.python.org/2/howto/doanddont.html#except

Copy link
Member Author

Choose a reason for hiding this comment

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

That's helpful -- thank you!

# The second array doesn't make sense, or it doesn't exist
second = np.arange(len(y))
x = second

# Popping some defaults
linefmt = kwargs.pop('linefmt', 'b-')
markerfmt = kwargs.pop('markerfmt', 'bo')
basefmt = kwargs.pop('basefmt', 'r-')
bottom = kwargs.pop('bottom', None)
label = kwargs.pop('label', None)

markerline, = self.plot(x, y, markerfmt, label="_nolegend_")

if bottom is None:
Expand Down
10 changes: 4 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3004,23 +3004,21 @@ def stackplot(x, *args, **kwargs):
draw_if_interactive()
finally:
ax.hold(washold)

return ret

# This function was autogenerated by boilerplate.py. Do not edit as
# changes will be lost
@_autogen_docstring(Axes.stem)
def stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-', bottom=None,
label=None, hold=None):
def stem(*args, **kwargs):
ax = gca()
# allow callers to override the hold state by passing hold=True|False
washold = ax.ishold()

hold = kwargs.pop('hold', None)
if hold is not None:
ax.hold(hold)
try:
ret = ax.stem(x, y, linefmt=linefmt, markerfmt=markerfmt,
basefmt=basefmt, bottom=bottom, label=label)
ret = ax.stem(*args, **kwargs)
draw_if_interactive()
finally:
ax.hold(washold)
Expand Down
0