Skip to content

Commit 0a7800b

Browse files
committed
Adding backend to docstrings and signatures
1 parent d2fc7a8 commit 0a7800b

File tree

1 file changed

+195
-7
lines changed

1 file changed

+195
-7
lines changed

pandas/plotting/_core.py

+195-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
from pandas._config import get_option
55

6-
from pandas.util._decorators import Appender
7-
86
from pandas.core.dtypes.common import is_integer, is_list_like
97
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
108

@@ -22,6 +20,7 @@ def hist_series(
2220
yrot=None,
2321
figsize=None,
2422
bins=10,
23+
backend=None,
2524
**kwargs
2625
):
2726
"""
@@ -50,6 +49,11 @@ def hist_series(
5049
bin edges are calculated and returned. If bins is a sequence, gives
5150
bin edges, including left edge of first bin and right edge of last
5251
bin. In this case, bins is returned unmodified.
52+
backend : str, default None
53+
Backend to use instead of the backend specified in the option
54+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
55+
specify the ``plotting.backend`` for the whole session, set
56+
``pd.options.plotting.backend``.
5357
**kwargs
5458
To be passed to the actual plotting function.
5559
@@ -62,7 +66,7 @@ def hist_series(
6266
--------
6367
matplotlib.axes.Axes.hist : Plot a histogram using matplotlib.
6468
"""
65-
plot_backend = _get_plot_backend(kwds.pop("backend", None))
69+
plot_backend = _get_plot_backend(backend)
6670
return plot_backend.hist_series(
6771
self,
6872
by=by,
@@ -93,6 +97,7 @@ def hist_frame(
9397
figsize=None,
9498
layout=None,
9599
bins=10,
100+
backend=None,
96101
**kwargs
97102
):
98103
"""
@@ -145,6 +150,11 @@ def hist_frame(
145150
bin edges are calculated and returned. If bins is a sequence, gives
146151
bin edges, including left edge of first bin and right edge of last
147152
bin. In this case, bins is returned unmodified.
153+
backend : str, default None
154+
Backend to use instead of the backend specified in the option
155+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
156+
specify the ``plotting.backend`` for the whole session, set
157+
``pd.options.plotting.backend``.
148158
**kwargs
149159
All other plotting keyword arguments to be passed to
150160
:meth:`matplotlib.pyplot.hist`.
@@ -172,7 +182,7 @@ def hist_frame(
172182
... }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])
173183
>>> hist = df.hist(bins=3)
174184
"""
175-
plot_backend = _get_plot_backend(kwds.pop("backend", None))
185+
plot_backend = _get_plot_backend(backend)
176186
return plot_backend.hist_frame(
177187
data,
178188
column=column,
@@ -383,7 +393,6 @@ def boxplot(
383393
)
384394

385395

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

0 commit comments

Comments
 (0)