From eb953ba8f913c79e2cf5286ef4be4e2f1ae6ecea Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Wed, 22 Aug 2012 23:26:16 +0100 Subject: [PATCH 1/5] Make Axes.stem take at least one argument. When it takes only one, the abscissae default to np.arange(len(y)) --- lib/matplotlib/axes.py | 27 ++++++++++++++++++++++++--- lib/matplotlib/pyplot.py | 10 ++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index ff7187b79740..882b082767c5 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -5074,13 +5074,13 @@ 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* @@ -5088,6 +5088,8 @@ def stem(self, x, y, linefmt='b-', markerfmt='bo', basefmt='r-', 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*). @@ -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: + # 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: diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 4dd2f44504ba..436ea0cc63bd 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -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) From 6973b56deb1197b6a5748f705fca6202e016a666 Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Tue, 13 Nov 2012 10:33:20 -0600 Subject: [PATCH 2/5] Add axes.stem call signature test --- lib/matplotlib/tests/test_axes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 1b7b15b31898..550db3101a21 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -901,6 +901,20 @@ def test_hist_stacked_weighted(): ax = fig.add_subplot(111) ax.hist( (d1, d2), weights=(w1,w2), histtype="stepfilled", stacked=True) +@cleanup +def test_stem_args(): + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + + x = range(10) + y = range(10) + + # Test the call signatures + ax.stem(y) + ax.stem(x, y) + ax.stem(x, y, 'r--') + ax.stem(x, y, 'r--', basefmt='b--') + @image_comparison(baseline_images=['transparent_markers'], remove_text=True) def test_transparent_markers(): np.random.seed(0) From 6c0299350abd8c47f46d54fcd47b9472ce8f1a9f Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Mon, 12 Nov 2012 23:18:09 -0600 Subject: [PATCH 3/5] Update CHANGELOG with axes.stem change --- CHANGELOG | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 19f1e06299fc..111a1708f053 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,10 @@ Also added some tests for the normalization class. Till Stensitzki +2012-11-12 Make axes.stem take at least one argument. + Uses a default range(n) when the first arg not provided. + Damon McDougall + 2012-11-09 Make plt.subplot() without arguments act as subplot(111) - PI 2012-10-05 Add support for saving animations as animated GIFs. - JVDP From db493b5c24769d701f68531cba4971c36743429d Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Fri, 16 Nov 2012 22:19:50 -0600 Subject: [PATCH 4/5] Fix positional argument parsing --- lib/matplotlib/axes.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 882b082767c5..533fcee2842e 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -5108,20 +5108,32 @@ def stem(self, *args, **kwargs): # Assume there's at least one data array y = np.asarray(args[0], dtype=np.float) + args = args[1:] # Try a second one try: - second = np.asarray(args[1], dtype=np.float) + second = np.asarray(args[0], dtype=np.float) x, y = y, second + args = args[1:] except: # 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-') + try: + linefmt = kwargs.pop('linefmt', args[0]) + except IndexError: + linefmt = kwargs.pop('linefmt', 'b-') + try: + markerfmt = kwargs.pop('markerfmt', args[1]) + except IndexError: + markerfmt = kwargs.pop('markerfmt', 'bo') + try: + basefmt = kwargs.pop('basefmt', args[2]) + except IndexError: + basefmt = kwargs.pop('basefmt', 'r-') + bottom = kwargs.pop('bottom', None) label = kwargs.pop('label', None) From 94f640e2422e3bd73769f9355b3381d66e25e264 Mon Sep 17 00:00:00 2001 From: Damon McDougall Date: Fri, 16 Nov 2012 22:21:27 -0600 Subject: [PATCH 5/5] Make stem catch explicit Exception classes --- lib/matplotlib/axes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/axes.py b/lib/matplotlib/axes.py index 533fcee2842e..116c409214fc 100644 --- a/lib/matplotlib/axes.py +++ b/lib/matplotlib/axes.py @@ -5115,7 +5115,7 @@ def stem(self, *args, **kwargs): second = np.asarray(args[0], dtype=np.float) x, y = y, second args = args[1:] - except: + except (IndexError, ValueError): # The second array doesn't make sense, or it doesn't exist second = np.arange(len(y)) x = second