8000 Merge pull request #1139 from dmcdougall/stemarg · matplotlib/matplotlib@3b649cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b649cb

Browse files
committed
Merge pull request #1139 from dmcdougall/stemarg
Make Axes.stem take at least one argument.
2 parents 7ed17e3 + 94f640e commit 3b649cb

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
Also added some tests for the normalization class.
2222
Till Stensitzki
2323

24+
2012-11-12 Make axes.stem take at least one argument.
25+
Uses a default range(n) when the first arg not provided.
26+
Damon McDougall
27+
2428
2012-11-09 Make plt.subplot() without arguments act as subplot(111) - PI
2529

2630
2012-10-05 Add support for saving animations as animated GIFs. - JVDP

lib/matplotlib/axes.py

Lines changed: 36 additions & 3 deletions
Original file 10000 line numberDiff line numberDiff line change
@@ -5029,20 +5029,22 @@ def broken_barh(self, xranges, yrange, **kwargs):
50295029

50305030
return col
50315031

5032-
def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-',
5033-
bottom=None, label=None):
5032+
def stem(self, *args, **kwargs):
50345033
"""
50355034
Create a stem plot.
50365035
5037-
Call signature::
5036+
Call signatures::
50385037
5038+
stem(y, linefmt='b-', markerfmt='bo', basefmt='r-')
50395039
stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')
50405040
50415041
A stem plot plots vertical lines (using *linefmt*) at each *x*
50425042
location from the baseline to *y*, and places a marker there
50435043
using *markerfmt*. A horizontal line at 0 is is plotted using
50445044
*basefmt*.
50455045
5046+
If no *x* values are provided, the default is (0, 1, ..., len(y) - 1)
5047+
50465048
Return value is a tuple (*markerline*, *stemlines*,
50475049
*baseline*).
50485050
@@ -5061,6 +5063,37 @@ def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-',
50615063
self.cla()
50625064
self.hold(True)
50635065

5066+
# Assume there's at least one data array
5067+
y = np.asarray(args[0], dtype=np.float)
5068+
args = args[1:]
5069+
5070+
# Try a second one
5071+
try:
5072+
second = np.asarray(args[0], dtype=np.float)
5073+
x, y = y, second
5074+
args = args[1:]
5075+
except (IndexError, ValueError):
5076+
# The second array doesn't make sense, or it doesn't exist
5077+
second = np.arange(len(y))
5078+
x = second
5079+
5080+
# Popping some defaults
5081+
try:
5082+
linefmt = kwargs.pop('linefmt', args[0])
5083+
except IndexError:
5084+
linefmt = kwargs.pop('linefmt', 'b-')
5085+
try:
5086+
markerfmt = kwargs.pop('markerfmt', args[1])
5087+
except IndexError:
5088+
markerfmt = kwargs.pop('markerfmt', 'bo')
5089+
try:
5090+
basefmt = kwargs.pop('basefmt', args[2])
5091+
except IndexError:
5092+
basefmt = kwargs.pop('basefmt', 'r-')
5093+
5094+
bottom = kwargs.pop('bottom', None)
5095+
label = kwargs.pop('label', None)
5096+
50645097
markerline, = self.plot(x, y, markerfmt, label="_nolegend_")
50655098

50665099
if bottom is None:

lib/matplotlib/pyplot.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,23 +2998,21 @@ def stackplot(x, *args, **kwargs):
29982998
draw_if_interactive()
29992999
finally:
30003000
ax.hold(washold)
3001-
3001+
30023002
return ret
30033003

30043004
# This function was autogenerated by boilerplate.py. Do not edit as
30053005
# changes will be lost
30063006
@_autogen_docstring(Axes.stem)
3007-
def stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-', bottom=None,
3008-
label=None, hold=None):
3007+
def stem(*args, **kwargs):
30093008
ax = gca()
30103009
# allow callers to override the hold state by passing hold=True|False
30113010
washold = ax.ishold()
3012-
3011+
hold = kwargs.pop('hold', None)
30133012
if hold is not None:
30143013
ax.hold(hold)
30153014
try:
3016-
ret = ax.stem(x, y, linefmt=linefmt, markerfmt=markerfmt,
3017-
basefmt=basefmt, bottom=bottom, label=label)
3015+
ret = ax.stem(*args, **kwargs)
30183016
draw_if_interactive()
30193017
finally:
30203018
ax.hold(washold)

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,20 @@ def test_hist_stacked_weighted():
924924
ax = fig.add_subplot(111)
925925
ax.hist( (d1, d2), weights=(w1,w2), histtype="stepfilled", stacked=True)
926926

927+
@cleanup
928+
def test_stem_args():
929+
fig = plt.figure()
930+
ax = fig.add_subplot(1, 1, 1)
931+
932+
x = range(10)
933+
y = range(10)
934+
935+
# Test the call signatures
936+
ax.stem(y)
937+
ax.stem(x, y)
938+
ax.stem(x, y, 'r--')
939+
ax.stem(x, y, 'r--', basefmt='b--')
940+
927941
@image_comparison(baseline_images=['transparent_markers'], remove_text=True)
928942
def test_transparent_markers():
929943
np.random.seed(0)

0 commit comments

Comments
 (0)
0