Skip to content

Commit 2199210

Browse files
committed
Adding backend to docstrings and signatures
1 parent 526e360 commit 2199210

File tree

1 file changed

+195
-6
lines changed

1 file changed

+195
-6
lines changed

pandas/plotting/_core.py

+195-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pandas._config import get_option
55

66
from pandas.compat._optional import import_optional_dependency
7-
from pandas.util._decorators import Appender
87

98
from pandas.core.dtypes.common import is_integer, is_list_like
109
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
@@ -28,6 +27,7 @@ def hist_series(
2827
yrot=None,
2928
figsize=None,
3029
bins=10,
30+
backend=None,
3131
**kwargs
3232
):
3333
"""
@@ -56,6 +56,11 @@ def hist_series(
5656
bin edges are calculated and returned. If bins is a sequence, gives
5757
bin edges, including left edge of first bin and right edge of last
5858
bin. In this case, bins is returned unmodified.
59+
backend : str, default None
60+
Backend to use instead of the backend specified in the option
61+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
62+
specify the ``plotting.backend`` for the whole session, set
63+
``pd.options.plotting.backend``.
5964
**kwargs
6065
To be passed to the actual plotting function
6166
@@ -68,7 +73,7 @@ def hist_series(
6873
--------
6974
matplotlib.axes.Axes.hist : Plot a histogram using matplotlib.
7075
"""
71-
plot_backend = _get_plot_backend(kwds.pop("backend", None))
76+
plot_backend = _get_plot_backend(backend)
7277
return plot_backend.hist_series(
7378
self,
7479
by=by,
@@ -99,6 +104,7 @@ def hist_frame(
99104
figsize=None,
100105
layout=None,
101106
bins=10,
107+
backend=None,
102108
**kwargs
103109
):
104110
"""
@@ -151,6 +157,11 @@ def hist_frame(
151157
bin edges are calculated and returned. If bins is a sequence, gives
152158
bin edges, including left edge of first bin and right edge of last
153159
bin. In this case, bins is returned unmodified.
160+
backend : str, default None
161+
Backend to use instead of the backend specified in the option
162+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
163+
specify the ``plotting.backend`` for the whole session, set
164+
``pd.options.plotting.backend``.
154165
**kwargs
155166
All other plotting keyword arguments to be passed to
156167
:meth:`matplotlib.pyplot.hist`.
@@ -178,7 +189,7 @@ def hist_frame(
178189
... }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])
179190
>>> hist = df.hist(bins=3)
180191
"""
181-
plot_backend = _get_plot_backend(kwds.pop("backend", None))
192+
plot_backend = _get_plot_backend(backend)
182193
return plot_backend.hist_frame(
183194
data,
184195
column=column,
@@ -389,7 +400,6 @@ def boxplot(
389400
)
390401

391402

392-
@Appender(boxplot.__doc__)
393403
def boxplot_frame(
394404
self,
395405
column=None,
@@ -401,9 +411,177 @@ def boxplot_frame(
401411
figsize=None,
402412
layout=None,
403413
return_type=None,
414+
backend=None,
404415
**kwargs
405416
):
406-
plot_backend = _get_plot_backend(kwds.pop("backend", None))
417+
"""
418+
Make a box plot from DataFrame columns.
419+
420+
Make a box-and-whisker plot from DataFrame columns, optionally grouped
421+
by some other columns. A box plot is a method for graphically depicting
422+
groups of numerical data through their quartiles.
423+
The box extends from the Q1 to Q3 quartile values of the data,
424+
with a line at the median (Q2). The whiskers extend from the edges
425+
of box to show the range of the data. The position of the whiskers
426+
is set by default to `1.5 * IQR (IQR = Q3 - Q1)` from the edges of the box.
427+
Outlier points are those past the end of the whiskers.
428+
429+
For further details see
430+
Wikipedia's entry for `boxplot <https://en.wikipedia.org/wiki/Box_plot>`_.
431+
432+
Parameters
433+
----------
434+
column : str or list of str, optional
435+
Column name or list of names, or vector.
436+
Can be any valid input to :meth:`pandas.DataFrame.groupby`.
437+
by : str or array-like, optional
438+
Column in the DataFrame to :meth:`pandas.DataFrame.groupby`.
439+
One box-plot will be done per value of columns in `by`.
440+
ax : object of class matplotlib.axes.Axes, optional
441+
The matplotlib axes to be used by boxplot.
442+
fontsize : float or str
443+
Tick label font size in points or as a string (e.g., `large`).
444+
rot : int or float, default 0
445+
The rotation angle of labels (in degrees)
446+
with respect to the screen coordinate system.
447+
grid : bool, default True
448+
Setting this to True will show the grid.
449+
figsize : A tuple (width, height) in inches
450+
The size of the figure to create in matplotlib.
451+
layout : tuple (rows, columns), optional
452+
For example, (3, 5) will display the subplots
453+
using 3 columns and 5 rows, starting from the top-left.
454+
return_type : {'axes', 'dict', 'both'} or None, default 'axes'
455+
The kind of object to return. The default is ``axes``.
456+
457+
* 'axes' returns the matplotlib axes the boxplot is drawn on.
458+
* 'dict' returns a dictionary whose values are the matplotlib
459+
Lines of the boxplot.
460+
* 'both' returns a namedtuple with the axes and dict.
461+
* when grouping with ``by``, a Series mapping columns to
462+
``return_type`` is returned.
463+
464+
If ``return_type`` is `None`, a NumPy array
465+
of axes with the same shape as ``layout`` is returned.
466+
backend : str, default None
467+
Backend to use instead of the backend specified in the option
468+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
469+
specify the ``plotting.backend`` for the whole session, set
470+
``pd.options.plotting.backend``.
471+
**kwds
472+
All other plotting keyword arguments to be passed to
473+
:func:`matplotlib.pyplot.boxplot`.
474+
475+
Returns
476+
-------
477+
result
478+
See Notes.
479+
480+
See Also
481+
--------
482+
Series.plot.hist: Make a histogram.
483+
matplotlib.pyplot.boxplot : Matplotlib equivalent plot.
484+
485+
Notes
486+
-----
487+
The return type depends on the `return_type` parameter:
488+
489+
* 'axes' : object of class matplotlib.axes.Axes
490+
* 'dict' : dict of matplotlib.lines.Line2D objects
491+
* 'both' : a namedtuple with structure (ax, lines)
492+
493+
For data grouped with ``by``, return a Series of the above or a numpy
494+
array:
495+
496+
* :class:`~pandas.Series`
497+
* :class:`~numpy.array` (for ``return_type = None``)
498+
499+
Use ``return_type='dict'`` when you want to tweak the appearance
500+
of the lines after plotting. In this case a dict containing the Lines
501+
making up the boxes, caps, fliers, medians, and whiskers is returned.
502+
503+
Examples
504+
--------
505+
506+
Boxplots can be created for every column in the dataframe
507+
by ``df.boxplot()`` or indicating the columns to be used:
508+
509+
.. plot::
510+
:context: close-figs
511+
512+
>>> np.random.seed(1234)
513+
>>> df = pd.DataFrame(np.random.randn(10,4),
514+
... columns=['Col1', 'Col2', 'Col3', 'Col4'])
515+
>>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3'])
516+
517+
Boxplots of variables distributions grouped by the values of a third
518+
variable can be created using the option ``by``. For instance:
519+
520+
.. plot::
521+
:context: close-figs
522+
523+
>>> df = pd.DataFrame(np.random.randn(10, 2),
524+
... columns=['Col1', 'Col2'])
525+
>>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
526+
... 'B', 'B', 'B', 'B', 'B'])
527+
>>> boxplot = df.boxplot(by='X')
528+
529+
A list of strings (i.e. ``['X', 'Y']``) can be passed to boxplot
530+
in order to group the data by combination of the variables in the x-axis:
531+
532+
.. plot::
533+
:context: close-figs
534+
535+
>>> df = pd.DataFrame(np.random.randn(10,3),
536+
... columns=['Col1', 'Col2', 'Col3'])
537+
>>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
538+
... 'B', 'B', 'B', 'B', 'B'])
539+
>>> df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A',
540+
... 'B', 'A', 'B', 'A', 'B'])
541+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])
542+
543+
The layout of boxplot can be adjusted giving a tuple to ``layout``:
544+
545+
.. plot::
546+
:context: close-figs
547+
548+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
549+
... layout=(2, 1))
550+
551+
Additional formatting can be done to the boxplot, like suppressing the grid
552+
(``grid=False``), rotating the labels in the x-axis (i.e. ``rot=45``)
553+
or changing the fontsize (i.e. ``fontsize=15``):
554+
555+
.. plot::
556+
:context: close-figs
557+
558+
>>> boxplot = df.boxplot(grid=False, rot=45, fontsize=15)
559+
560+
The parameter ``return_type`` can be used to select the type of element
561+
returned by `boxplot`. When ``return_type='axes'`` is selected,
562+
the matplotlib axes on which the boxplot is drawn are returned:
563+
564+
>>> boxplot = df.boxplot(column=['Col1','Col2'], return_type='axes')
565+
>>> type(boxplot)
566+
<class 'matplotlib.axes._subplots.AxesSubplot'>
567+
568+
When grouping with ``by``, a Series mapping columns to ``return_type``
569+
is returned:
570+
571+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
572+
... return_type='axes')
573+
>>> type(boxplot)
574+
<class 'pandas.core.series.Series'>
575+
576+
If ``return_type`` is `None`, a NumPy array of axes with the same shape
577+
as ``layout`` is returned:
578+
579+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
580+
... return_type=None)
581+
>>> type(boxplot)
582+
<class 'numpy.ndarray'>
583+
"""
584+
plot_backend = _get_plot_backend(backend)
407585
return plot_backend.boxplot_frame(
408586
self,
409587
column=column,
@@ -431,6 +609,7 @@ def boxplot_frame_groupby(
431609
layout=None,
432610
sharex=False,
433611
sharey=True,
612+
backend=None,
434613
**kwargs
435614
):
436615
"""
@@ -459,6 +638,11 @@ def boxplot_frame_groupby(
459638
Whether y-axes will be shared among subplots
460639
461640
.. versionadded:: 0.23.1
641+
backend : str, default None
642+
Backend to use instead of the backend specified in the option
643+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
644+
specify the ``plotting.backend`` for the whole session, set
645+
``pd.options.plotting.backend``.
462646
**kwargs
463647
All other plotting keyword arguments to be passed to
464648
matplotlib's boxplot function
@@ -482,7 +666,7 @@ def boxplot_frame_groupby(
482666
>>> grouped = df.unstack(level='lvl1').groupby(level=0, axis=1)
483667
>>> boxplot_frame_groupby(grouped, subplots=False)
484668
"""
485-
plot_backend = _get_plot_backend(kwds.pop("backend", None))
669+
plot_backend = _get_plot_backend(backend)
486670
return plot_backend.boxplot_frame_groupby(
487671
grouped,
488672
subplots=subplots,
@@ -586,6 +770,11 @@ class PlotAccessor(PandasObject):
586770
labels with "(right)" in the legend
587771
include_bool : bool, default is False
588772
If True, boolean values can be plotted.
773+
backend : str, default None
774+
Backend to use instead of the backend specified in the option
775+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
776+
specify the ``plotting.backend`` for the whole session, set
777+
``pd.options.plotting.backend``.
589778
**kwargs
590779
Options to pass to matplotlib plotting method.
591780

0 commit comments

Comments
 (0)