39
39
import matplotlib .transforms as mtransforms
40
40
import matplotlib .tri as mtri
41
41
from matplotlib .cbook import (
42
- _backports , mplDeprecation , STEP_LOOKUP_MAP , iterable , safe_first_element )
42
+ _backports , mplDeprecation , warn_deprecated ,
43
+ STEP_LOOKUP_MAP , iterable , safe_first_element )
43
44
from matplotlib .container import BarContainer , ErrorbarContainer , StemContainer
44
45
from matplotlib .axes ._base import _AxesBase , _process_plot_format
45
46
@@ -2515,27 +2516,109 @@ def stem(self, *args, **kwargs):
2515
2516
"""
2516
2517
Create a stem plot.
2517
2518
2518
- Call signatures::
2519
+ A stem plot plots vertical lines at each *x* location from the baseline
2520
+ to *y*, and places a marker there.
2521
+
2522
+ Call signature::
2523
+
2524
+ stem([x,] y, linefmt=None, markerfmt=None, basefmt=None)
2525
+
2526
+ The x-positions are optional. The formats may be provided either as
2527
+ positional or as keyword-arguments.
2528
+
2529
+ Parameters
2530
+ ----------
2531
+ x : array-like, optional
2532
+ The x-positions of the stems. Default: (0, 1, ..., len(y) - 1).
2533
+
2534
+ y : array-like
2535
+ The y-values of the stem heads.
2536
+
2537
+ linefmt : str, optional
2538
+ A string defining the properties of the vertical lines. Usually,
2539
+ this will be a color or a color and a linestyle:
2540
+
2541
+ ========= =============
2542
+ Character Line Style
2543
+ ========= =============
2544
+ ``'-'`` solid line
2545
+ ``'--'`` dashed line
2546
+ ``'-.'`` dash-dot line
2547
+ ``':'`` dotted line
2548
+ ========= =============
2549
+
2550
+ Default: 'C0-', i.e. solid line with the first color of the color
2551
+ cycle.
2519
2552
2520
- stem(y, linefmt='b-', markerfmt='bo', basefmt='r-')
2521
- stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')
2553
+ Note: While it is technically possible to specify valid formats
2554
+ other than color or color and linestyle (e.g. 'rx' or '-.'), this
2555
+ is beyond the intention of the method and will most likely not
2556
+ result in a reasonable reasonable plot.
2522
2557
2523
- A stem plot plots vertical lines (using *linefmt*) at each *x*
2524
- location from the baseline to *y*, and places a marker there
2525
- using *markerfmt*. A horizontal line at 0 is plotted using
2526
- *basefmt* .
2558
+ markerfmt : str, optional
2559
+ A string defining the properties of the markers at the stem heads.
2560
+ Default: 'C0o', i.e. filled circles with the first color of the
2561
+ color cycle .
2527
2562
2528
- If no *x* values are provided, the default is (0, 1, ..., len(y) - 1)
2563
+ basefmt : str, optional
2564
+ A format string defining the properties of the baseline.
2565
+
2566
+ Default: 'C3-' ('C2-' in classic mode).
2567
+
2568
+ bottom : float, optional, default: 0
2569
+ The y-position of the baseline.
2570
+
2571
+ label : str, optional, default: None
2572
+ The label to use for the stems in legends.
2573
+
2574
+
2575
+ Other Parameters
2576
+ ----------------
2577
+ **kwargs
2578
+ No other parameters are supported. They are currently ignored
2579
+ silently for backward compatibility. This behavior is deprecated.
2580
+ Future versions will not accept any other parameters and will
2581
+ raise a TypeError instead.
2582
+
2583
+
2584
+ Returns
2585
+ -------
2586
+ :class:`~matplotlib.container.StemContainer`
2587
+ The stemcontainer may be treated like a tuple
2588
+ (*markerline*, *stemlines*, *baseline*)
2529
2589
2530
- Return value is a tuple (*markerline*, *stemlines*,
2531
- *baseline*). See :class:`~matplotlib.container.StemContainer`
2590
+
2591
+ Notes
2592
+ -----
2532
2593
2533
2594
.. seealso::
2534
- This
2535
- `document <http://www.mathworks.com/help/techdoc/ref/stem.html>`_
2536
- for details .
2595
+ The MATLAB function
2596
+ `stem <http://www.mathworks.com/help/techdoc/ref/stem.html>`_
2597
+ which inspired this method .
2537
2598
2538
2599
"""
2600
+
2601
+ # kwargs handling
2602
+ # We would like to have a signature with explicit kewords:
2603
+ # stem(*args, linefmt=None, markerfmt=None, basefmt=None,
2604
+ # bottom=0, label=None)
2605
+ # Unfortunately, this is not supported in Python 2.x. There, *args
2606
+ # can only exist after keyword arguments.
2607
+ linefmt = kwargs .pop ('linefmt' , None )
2608
+ markerfmt = kwargs .pop ('markerfmt' , None )
2609
+ basefmt = kwargs .pop ('basefmt' , None )
2610
+ bottom = kwargs .pop ('bottom' , None )
2611
+ if bottom is None :
2612
+ bottom = 0
2613
+ label = kwargs .pop ('label' , None )
2614
+ if kwargs :
2615
+ warn_deprecated (since = '2.2' ,
2616
+ message = "stem() got an unexpected keyword "
2617
+ "argument '%s'. This will raise a "
2618
+ "TypeError in future versions." % (
2619
+ next (k for k in kwargs ), )
2620
+ )
2621
+
2539
2622
remember_hold = self ._hold
2540
2623
if not self ._hold :
2541
2624
self .cla ()
@@ -2555,11 +2638,10 @@ def stem(self, *args, **kwargs):
2555
2638
second = np .arange (len (y ))
2556
2639
x = second
2557
2640
2558
- # Popping some defaults
2559
- try :
2560
- linefmt = kwargs ['linefmt' ]
2561
- except KeyError :
2641
+ # defaults for formats
2642
+ if linefmt is None :
2562
2643
try :
2644
+ # fallback to positional argument
2563
2645
linefmt = args [0 ]
2564
2646
except IndexError :
2565
2647
linecolor = 'C0'
@@ -2570,10 +2652,10 @@ def stem(self, *args, **kwargs):
2570
2652
_process_plot_format (linefmt )
2571
2653
else :
2572
2654
linestyle , linemarker , linecolor = _process_plot_format (linefmt )
2573
- try :
2574
- markerfmt = kwargs ['markerfmt' ]
2575
- except KeyError :
2655
+
2656
+ if markerfmt is None :
2576
2657
try :
2658
+ # fallback to positional argument
2577
2659
markerfmt = args [1 ]
2578
2660
except IndexError :
2579
2661
markercolor = 'C0'
@@ -2585,10 +2667,10 @@ def stem(self, *args, **kwargs):
2585
2667
else :
2586
2668
markerstyle , markermarker , markercolor = \
2587
2669
_process_plot_format (markerfmt )
2588
- try :
2589
- basefmt = kwargs ['basefmt' ]
2590
- except KeyError :
2670
+
2671
+ if basefmt is None :
2591
2672
try :
2673
+ # fallback to positional argument
2592
2674
basefmt = args [2 ]
2593
2675
except IndexError :
2594
2676
if rcParams ['_internal.classic_mode' ]:
@@ -2603,15 +2685,9 @@ def stem(self, *args, **kwargs):
2603
2685
else :
2604
2686
basestyle , basemarker , basecolor = _process_plot_format (basefmt )
2605
2687
2606
- bottom = kwargs .pop ('bottom' , None )
2607
- label = kwargs .pop ('label' , None )
2608
-
2609
2688
markerline , = self .plot (x , y , color = markercolor , linestyle = markerstyle ,
2610
2689
marker = markermarker , label = "_nolegend_" )
2611
2690
2612
- if bottom is None :
2613
- bottom = 0
2614
-
2615
2691
stemlines = []
2616
2692
for thisx , thisy in zip (x , y ):
2617
2693
l , = self .plot ([thisx , thisx ], [bottom , thisy ],
0 commit comments