4
4
from pandas ._config import get_option
5
5
6
6
from pandas .compat ._optional import import_optional_dependency
7
- from pandas .util ._decorators import Appender
8
7
9
8
from pandas .core .dtypes .common import is_integer , is_list_like
10
9
from pandas .core .dtypes .generic import ABCDataFrame , ABCSeries
@@ -28,6 +27,7 @@ def hist_series(
28
27
yrot = None ,
29
28
figsize = None ,
30
29
bins = 10 ,
30
+ backend = None ,
31
31
** kwds
32
32
):
33
33
"""
@@ -56,6 +56,11 @@ def hist_series(
56
56
bin edges are calculated and returned. If bins is a sequence, gives
57
57
bin edges, including left edge of first bin and right edge of last
58
58
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``.
59
64
`**kwds` : keywords
60
65
To be passed to the actual plotting function
61
66
@@ -68,7 +73,7 @@ def hist_series(
68
73
--------
69
74
matplotlib.axes.Axes.hist : Plot a histogram using matplotlib.
70
75
"""
71
- plot_backend = _get_plot_backend (kwds . pop ( " backend" , None ) )
76
+ plot_backend = _get_plot_backend (backend )
72
77
return plot_backend .hist_series (
73
78
self ,
74
79
by = by ,
@@ -99,6 +104,7 @@ def hist_frame(
99
104
figsize = None ,
100
105
layout = None ,
101
106
bins = 10 ,
107
+ backend = None ,
102
108
** kwds
103
109
):
104
110
"""
@@ -151,6 +157,11 @@ def hist_frame(
151
157
bin edges are calculated and returned. If bins is a sequence, gives
152
158
bin edges, including left edge of first bin and right edge of last
153
159
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``.
154
165
**kwds
155
166
All other plotting keyword arguments to be passed to
156
167
:meth:`matplotlib.pyplot.hist`.
@@ -178,7 +189,7 @@ def hist_frame(
178
189
... }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])
179
190
>>> hist = df.hist(bins=3)
180
191
"""
181
- plot_backend = _get_plot_backend (kwds . pop ( " backend" , None ) )
192
+ plot_backend = _get_plot_backend (backend )
182
193
return plot_backend .hist_frame (
183
194
data ,
184
195
column = column ,
@@ -389,7 +400,6 @@ def boxplot(
389
400
)
390
401
391
402
392
- @Appender (boxplot .__doc__ )
393
403
def boxplot_frame (
394
404
self ,
395
405
column = None ,
@@ -401,9 +411,177 @@ def boxplot_frame(
401
411
figsize = None ,
402
412
layout = None ,
403
413
return_type = None ,
414
+ backend = None ,
404
415
** kwds
405
416
):
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 )
407
585
return plot_backend .boxplot_frame (
408
586
self ,
409
587
column = column ,
@@ -431,6 +609,7 @@ def boxplot_frame_groupby(
431
609
layout = None ,
432
610
sharex = False ,
433
611
sharey = True ,
612
+ backend = None ,
434
613
** kwds
435
614
):
436
615
"""
@@ -459,6 +638,11 @@ def boxplot_frame_groupby(
459
638
Whether y-axes will be shared among subplots
460
639
461
640
.. 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``.
462
646
`**kwds` : Keyword Arguments
463
647
All other plotting keyword arguments to be passed to
464
648
matplotlib's boxplot function
@@ -482,7 +666,7 @@ def boxplot_frame_groupby(
482
666
>>> grouped = df.unstack(level='lvl1').groupby(level=0, axis=1)
483
667
>>> boxplot_frame_groupby(grouped, subplots=False)
484
668
"""
485
- plot_backend = _get_plot_backend (kwds . pop ( " backend" , None ) )
669
+ plot_backend = _get_plot_backend (backend )
486
670
return plot_backend .boxplot_frame_groupby (
487
671
grouped ,
488
672
subplots = subplots ,
@@ -586,6 +770,11 @@ class PlotAccessor(PandasObject):
586
770
labels with "(right)" in the legend
587
771
include_bool : bool, default is False
588
772
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``.
589
778
`**kwds` : keywords
590
779
Options to pass to matplotlib plotting method.
591
780
0 commit comments