Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e500d5b

Browse files
authored
Merge pull request #10154 from timhoffm/axes-stem
improve Axes.stem docstring
2 parents 364d4ca + 57b716e commit e500d5b

File tree

2 files changed

+152
-33
lines changed

2 files changed

+152
-33
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 106 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
import matplotlib.transforms as mtransforms
4040
import matplotlib.tri as mtri
4141
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)
4344
from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer
4445
from matplotlib.axes._base import _AxesBase, _process_plot_format
4546

@@ -2515,27 +2516,109 @@ def stem(self, *args, **kwargs):
25152516
"""
25162517
Create a stem plot.
25172518
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.
25192552
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.
25222557
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.
25272562
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*)
25292589
2530-
Return value is a tuple (*markerline*, *stemlines*,
2531-
*baseline*). See :class:`~matplotlib.container.StemContainer`
2590+
2591+
Notes
2592+
-----
25322593
25332594
.. 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.
25372598
25382599
"""
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+
25392622
remember_hold = self._hold
25402623
if not self._hold:
25412624
self.cla()
@@ -2555,11 +2638,10 @@ def stem(self, *args, **kwargs):
25552638
second = np.arange(len(y))
25562639
x = second
25572640

2558-
# Popping some defaults
2559-
try:
2560-
linefmt = kwargs['linefmt']
2561-
except KeyError:
2641+
# defaults for formats
2642+
if linefmt is None:
25622643
try:
2644+
# fallback to positional argument
25632645
linefmt = args[0]
25642646
except IndexError:
25652647
linecolor = 'C0'
@@ -2570,10 +2652,10 @@ def stem(self, *args, **kwargs):
25702652
_process_plot_format(linefmt)
25712653
else:
25722654
linestyle, linemarker, linecolor = _process_plot_format(linefmt)
2573-
try:
2574-
markerfmt = kwargs['markerfmt']
2575-
except KeyError:
2655+
2656+
if markerfmt is None:
25762657
try:
2658+
# fallback to positional argument
25772659
markerfmt = args[1]
25782660
except IndexError:
25792661
markercolor = 'C0'
@@ -2585,10 +2667,10 @@ def stem(self, *args, **kwargs):
25852667
else:
25862668
markerstyle, markermarker, markercolor = \
25872669
_process_plot_format(markerfmt)
2588-
try:
2589-
basefmt = kwargs['basefmt']
2590-
except KeyError:
2670+
2671+
if basefmt is None:
25912672
try:
2673+
# fallback to positional argument
25922674
basefmt = args[2]
25932675
except IndexError:
25942676
if rcParams['_internal.classic_mode']:
@@ -2603,15 +2685,9 @@ def stem(self, *args, **kwargs):
26032685
else:
26042686
basestyle, basemarker, basecolor = _process_plot_format(basefmt)
26052687

2606-
bottom = kwargs.pop('bottom', None)
2607-
label = kwargs.pop('label', None)
2608-
26092688
markerline, = self.plot(x, y, color=markercolor, linestyle=markerstyle,
26102689
marker=markermarker, label="_nolegend_")
26112690

2612-
if bottom is None:
2613-
bottom = 0
2614-
26152691
stemlines = []
26162692
for thisx, thisy in zip(x, y):
26172693
l, = self.plot([thisx, thisx], [bottom, thisy],

lib/matplotlib/container.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
class Container(tuple):
1111
"""
1212
Base class for containers.
13+
14+
Containers are classes that collect semantically related Artists such as
15+
the bars of a bar plot.
1316
"""
1417

1518
def __repr__(self):
@@ -107,6 +110,23 @@ def get_children(self):
107110

108111

109112
class BarContainer(Container):
113+
"""
114+
Container for the artists of bar plots (e.g. created by `.Axes.bar`).
115+
116+
The container can be treated as a tuple of the *patches* themselves.
117+
Additionally, you can access these and further parameters by the
118+
attributes.
119+
120+
Attributes
121+
----------
122+
patches : list of :class:`~matplotlib.patches.Rectangle`
123+
The artists of the bars.
124+
125+
errorbar : None or :class:`~matplotlib.container.ErrorbarContainer`
126+
A container for the error bar artists if error bars are present.
127+
*None* otherwise.
128+
129+
"""
110130

111131
def __init__(self, patches, errorbar=None, **kwargs):
112132
self.patches = patches
@@ -115,8 +135,12 @@ def __init__(self, patches, errorbar=None, **kwargs):
115135

116136

117137
class ErrorbarContainer(Container):
118-
'''
119-
Container for errobars.
138+
"""
139+
Container for the artists of error bars (e.g. created by `.Axes.errorbar`).
140+
141+
The container can be treated as the *lines* tuple itself.
142+
Additionally, you can access these and further parameters by the
143+
attributes.
120144
121145
Attributes
122146
----------
@@ -132,7 +156,8 @@ class ErrorbarContainer(Container):
132156
133157
has_xerr, has_yerr : bool
134158
``True`` if the errorbar has x/y errors.
135-
'''
159+
160+
"""
136161

137162
def __init__(self, lines, has_xerr=False, has_yerr=False, **kwargs):
138163
self.lines = lines
@@ -142,6 +167,24 @@ def __init__(self, lines, has_xerr=False, has_yerr=False, **kwargs):
142167

143168

144169
class StemContainer(Container):
170+
"""
171+
Container for the artists created in a :meth:`.Axes.stem` plot.
172+
173+
The container can be treated like a namedtuple ``(markerline, stemlines,
174+
baseline)``.
175+
176+
Attributes
177+
----------
178+
markerline : :class:`~matplotlib.lines.Line2D`
179+
The artist of the markers at the stem heads.
180+
181+
stemlines : list of :class:`~matplotlib.lines.Line2D`
182+
The artists of the vertical lines for all stems.
183+
184+
baseline : :class:`~matplotlib.lines.Line2D`
185+
The artist of the horizontal baseline.
186+
187+
"""
145188

146189
def __init__(self, markerline_stemlines_baseline, **kwargs):
147190
markerline, stemlines, baseline = markerline_stemlines_baseline

0 commit comments

Comments
 (0)