Skip to content

Commit e9afeb7

Browse files
committed
[WIP] DOC Fixes pandas-dev#8447 fixed issues and added examples for return_type
1 parent f42600f commit e9afeb7

File tree

1 file changed

+88
-32
lines changed

1 file changed

+88
-32
lines changed

pandas/plotting/_core.py

+88-32
Original file line numberDiff line numberDiff line change
@@ -2013,24 +2013,22 @@ def plot_series(data, kind='line', ax=None, # Series unique
20132013
----------
20142014
column : str or list of str, optional
20152015
Column name or list of names, or vector.
2016-
Can be any valid input to groupby.
2017-
by : str or array-like
2018-
Column in the DataFrame to groupby.
2019-
ax : object of class matplotlib.axes.Axes, default `None`
2016+
Can be any valid input to :meth:`pandas.DataFrame.groupby`.
2017+
by : str or array-like, optional
2018+
Column in the DataFrame to :meth:`pandas.DataFrame.groupby`.
2019+
One box-plot will be done per value of columns in `by`.
2020+
ax : object of class matplotlib.axes.Axes, optional
20202021
The matplotlib axes to be used by boxplot.
20212022
fontsize : float or str
2022-
Tick label font size in points or as a string (e.g., ‘large’)
2023-
(see `matplotlib.axes.Axes.tick_params
2024-
<https://matplotlib.org/api/_as_gen/
2025-
matplotlib.axes.Axes.tick_params.html>`_).
2023+
Tick label font size in points or as a string (e.g., ‘large’).
20262024
rot : int or float, default 0
20272025
The rotation angle of labels (in degrees)
20282026
with respect to the screen coordinate sytem.
2029-
grid : boolean, default `True`
2027+
grid : boolean, default True
20302028
Setting this to True will show the grid.
20312029
figsize : A tuple (width, height) in inches
20322030
The size of the figure to create in matplotlib.
2033-
layout : tuple (rows, columns) (optional)
2031+
layout : tuple (rows, columns), optional
20342032
For example, (3, 5) will display the subplots
20352033
using 3 columns and 5 rows, starting from the top-left.
20362034
return_type : {None, 'axes', 'dict', 'both'}, default 'axes'
@@ -2041,22 +2039,13 @@ def plot_series(data, kind='line', ax=None, # Series unique
20412039
Lines of the boxplot.
20422040
* 'both' returns a namedtuple with the axes and dict.
20432041
* when grouping with ``by``, a Series mapping columns to
2044-
``return_type`` is returned (i.e.
2045-
``df.boxplot(column=['Col1','Col2'], by='var',return_type='axes')``
2046-
may return ``Series([AxesSubplot(..),AxesSubplot(..)],
2047-
index=['Col1','Col2'])``).
2042+
``return_type`` is returned.
20482043
20492044
If ``return_type`` is `None`, a NumPy array
2050-
of axes with the same shape as ``layout`` is returned
2051-
(i.e. ``df.boxplot(column=['Col1','Col2'],
2052-
by='var',return_type=None)`` may return a
2053-
``array([<matplotlib.axes._subplots.AxesSubplot object at ..>,
2054-
<matplotlib.axes._subplots.AxesSubplot object at ..>],
2055-
dtype=object)``).
2056-
**kwds : Keyword Arguments (optional)
2045+
of axes with the same shape as ``layout`` is returned.
2046+
**kwds : Keyword Arguments, optional
20572047
All other plotting keyword arguments to be passed to
2058-
`matplotlib.pyplot.boxplot <https://matplotlib.org/api/_as_gen/
2059-
matplotlib.pyplot.boxplot.html#matplotlib.pyplot.boxplot>`_.
2048+
:func:`matplotlib.pyplot.boxplot`.
20602049
20612050
Returns
20622051
-------
@@ -2074,8 +2063,8 @@ def plot_series(data, kind='line', ax=None, # Series unique
20742063
20752064
See Also
20762065
--------
2077-
matplotlib.pyplot.boxplot: Make a box and whisker plot.
2078-
matplotlib.pyplot.hist: Make a hsitogram.
2066+
matplotlib.pyplot.boxplot : Make a box and whisker plot.
2067+
matplotlib.pyplot.hist : Make a histogram.
20792068
20802069
Notes
20812070
-----
@@ -2093,27 +2082,27 @@ def plot_series(data, kind='line', ax=None, # Series unique
20932082
:context: close-figs
20942083
20952084
>>> np.random.seed(1234)
2096-
>>> df = pd.DataFrame(np.random.rand(10,4),
2085+
>>> df = pd.DataFrame(np.random.randn(10,4),
20972086
... columns=['Col1', 'Col2', 'Col3', 'Col4'])
20982087
>>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3'])
20992088
2100-
Boxplots of variables distributions grouped by a third variable values
2101-
can be created using the option ``by``. For instance:
2089+
Boxplots of variables distributions grouped by the values of a third
2090+
variable can be created using the option ``by``. For instance:
21022091
21032092
.. plot::
21042093
:context: close-figs
21052094
2106-
>>> df = pd.DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'] )
2095+
>>> df = pd.DataFrame(np.random.randn(10,2), columns=['Col1', 'Col2'] )
21072096
>>> df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])
21082097
>>> boxplot = df.boxplot(by='X')
21092098
2110-
A list of strings (i.e. ``['X','Y']``) containing can be passed to boxplot
2099+
A list of strings (i.e. ``['X','Y']``) can be passed to boxplot
21112100
in order to group the data by combination of the variables in the x-axis:
21122101
21132102
.. plot::
21142103
:context: close-figs
21152104
2116-
>>> df = pd.DataFrame(np.random.rand(10,3),
2105+
>>> df = pd.DataFrame(np.random.randn(10,3),
21172106
... columns=['Col1', 'Col2', 'Col3'])
21182107
>>> df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])
21192108
>>> df['Y'] = pd.Series(['A','B','A','B','A','B','A','B','A','B'])
@@ -2124,7 +2113,7 @@ def plot_series(data, kind='line', ax=None, # Series unique
21242113
.. plot::
21252114
:context: close-figs
21262115
2127-
>>> df = pd.DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'])
2116+
>>> df = pd.DataFrame(np.random.randn(10,2), columns=['Col1', 'Col2'])
21282117
>>> df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])
21292118
>>> boxplot = df.boxplot(by='X', layout=(2,1))
21302119
@@ -2136,6 +2125,73 @@ def plot_series(data, kind='line', ax=None, # Series unique
21362125
:context: close-figs
21372126
21382127
>>> boxplot = df.boxplot(grid=False, rot=45, fontsize=15)
2128+
2129+
2130+
The parameter ``return_type`` can be used to select the type of element
2131+
returned by `boxplot`. When ``return_type='axes'`` is selected,
2132+
the matplotlib axes on which the boxplot is drawn are returned:
2133+
2134+
>>> df.boxplot(column=['Col1','Col2'], return_type='axes')
2135+
<matplotlib.axes._subplots.AxesSubplot object at ...>
2136+
2137+
If selecting ``return_type='dict'`` a dictionary containing the
2138+
lines is returned:
2139+
2140+
>>> df.boxplot(column=['Col1','Col2'], return_type='dict')
2141+
{'boxes': [<matplotlib.lines.Line2D at ...>,
2142+
<matplotlib.lines.Line2D at ...>],
2143+
'caps': [<matplotlib.lines.Line2D at ...>,
2144+
<matplotlib.lines.Line2D at ...>,
2145+
<matplotlib.lines.Line2D at ...>,
2146+
<matplotlib.lines.Line2D at ...>],
2147+
'fliers': [<matplotlib.lines.Line2D at ...>,
2148+
<matplotlib.lines.Line2D at ...>],
2149+
'means': [],
2150+
'medians': [<matplotlib.lines.Line2D at ...>,
2151+
<matplotlib.lines.Line2D at ...>],
2152+
'whiskers': [<matplotlib.lines.Line2D at ...>,
2153+
<matplotlib.lines.Line2D at ...>,
2154+
<matplotlib.lines.Line2D at ...>,
2155+
<matplotlib.lines.Line2D at ...>]}
2156+
2157+
If selecting ``return_type='both'``, a namedtuple with matplotlib axes and
2158+
Line objets is returned:
2159+
2160+
>>> df.boxplot(column=['Col1','Col2'], return_type='both')
2161+
Boxplot(ax=<matplotlib.axes._subplots.AxesSubplot object
2162+
at 0x115821128>, lines={'whiskers':
2163+
[<matplotlib.lines.Line2D object at ...>,
2164+
<matplotlib.lines.Line2D object at ...>,
2165+
<matplotlib.lines.Line2D object at ...>,
2166+
<matplotlib.lines.Line2D object at ...>],
2167+
'caps': [<matplotlib.lines.Line2D object at ...>,
2168+
<matplotlib.lines.Line2D object at ...>,
2169+
<matplotlib.lines.Line2D object at ...>,
2170+
<matplotlib.lines.Line2D object at ...>],
2171+
'boxes': [<matplotlib.lines.Line2D object at ...>,
2172+
<matplotlib.lines.Line2D object at ...>],
2173+
'medians': [<matplotlib.lines.Line2D object at ...>,
2174+
<matplotlib.lines.Line2D object at ...>],
2175+
'fliers': [<matplotlib.lines.Line2D object at ...>,
2176+
<matplotlib.lines.Line2D object at ...>], 'means': []})
2177+
2178+
When grouping with ``by``, a Series mapping columns to ``return_type``
2179+
is returned:
2180+
2181+
2182+
>>> df.boxplot(column=['Col1','Col2'], by='X', return_type='axes')
2183+
Col1 AxesSubplot(0.1,0.15;0.363636x0.75)
2184+
Col2 AxesSubplot(0.536364,0.15;0.363636x0.75)
2185+
dtype: object
2186+
2187+
If ``return_type`` is `None`, a NumPy array of axes with the same shape
2188+
as ``layout`` is returned:
2189+
2190+
>>> df.boxplot(column=['Col1','Col2'], by='X', return_type=None)
2191+
array([<matplotlib.axes._subplots.AxesSubplot object at ...>,
2192+
<matplotlib.axes._subplots.AxesSubplot object at ...>],
2193+
dtype=object)
2194+
21392195
"""
21402196

21412197
@Appender(_shared_docs['boxplot'] % _shared_doc_kwargs)

0 commit comments

Comments
 (0)