3
3
4
4
from pandas ._config import get_option
5
5
6
- from pandas .util ._decorators import Appender
7
-
8
6
from pandas .core .dtypes .common import is_integer , is_list_like
9
7
from pandas .core .dtypes .generic import ABCDataFrame , ABCSeries
10
8
@@ -22,6 +20,7 @@ def hist_series(
22
20
yrot = None ,
23
21
figsize = None ,
24
22
bins = 10 ,
23
+ backend = None ,
25
24
** kwargs
26
25
):
27
26
"""
@@ -50,6 +49,11 @@ def hist_series(
50
49
bin edges are calculated and returned. If bins is a sequence, gives
51
50
bin edges, including left edge of first bin and right edge of last
52
51
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``.
53
57
**kwargs
54
58
To be passed to the actual plotting function.
55
59
@@ -62,7 +66,7 @@ def hist_series(
62
66
--------
63
67
matplotlib.axes.Axes.hist : Plot a histogram using matplotlib.
64
68
"""
65
- plot_backend = _get_plot_backend (kwds . pop ( " backend" , None ) )
69
+ plot_backend = _get_plot_backend (backend )
66
70
return plot_backend .hist_series (
67
71
self ,
68
72
by = by ,
@@ -93,6 +97,7 @@ def hist_frame(
93
97
figsize = None ,
94
98
layout = None ,
95
99
bins = 10 ,
100
+ backend = None ,
96
101
** kwargs
97
102
):
98
103
"""
@@ -145,6 +150,11 @@ def hist_frame(
145
150
bin edges are calculated and returned. If bins is a sequence, gives
146
151
bin edges, including left edge of first bin and right edge of last
147
152
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``.
148
158
**kwargs
149
159
All other plotting keyword arguments to be passed to
150
160
:meth:`matplotlib.pyplot.hist`.
@@ -172,7 +182,7 @@ def hist_frame(
172
182
... }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])
173
183
>>> hist = df.hist(bins=3)
174
184
"""
175
- plot_backend = _get_plot_backend (kwds . pop ( " backend" , None ) )
185
+ plot_backend = _get_plot_backend (backend )
176
186
return plot_backend .hist_frame (
177
187
data ,
178
188
column = column ,
@@ -383,7 +393,6 @@ def boxplot(
383
393
)
384
394
385
395
386
- @Appender (boxplot .__doc__ )
387
396
def boxplot_frame (
388
397
self ,
389
398
column = None ,
@@ -395,9 +404,177 @@ def boxplot_frame(
395
404
figsize = None ,
396
405
layout = None ,
397
406
return_type = None ,
407
+ backend = None ,
398
408
** kwargs
399
409
):
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 )
401
578
return plot_backend .boxplot_frame (
402
579
self ,
403
580
column = column ,
@@ -425,6 +602,7 @@ def boxplot_frame_groupby(
425
602
layout = None ,
426
603
sharex = False ,
427
604
sharey = True ,
605
+ backend = None ,
428
606
** kwargs
429
607
):
430
608
"""
@@ -454,6 +632,11 @@ def boxplot_frame_groupby(
454
632
Whether y-axes will be shared among subplots.
455
633
456
634
.. 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``.
457
640
**kwargs
458
641
All other plotting keyword arguments to be passed to
459
642
matplotlib's boxplot function.
@@ -477,7 +660,7 @@ def boxplot_frame_groupby(
477
660
>>> grouped = df.unstack(level='lvl1').groupby(level=0, axis=1)
478
661
>>> boxplot_frame_groupby(grouped, subplots=False)
479
662
"""
480
- plot_backend = _get_plot_backend (kwds . pop ( " backend" , None ) )
663
+ plot_backend = _get_plot_backend (backend )
481
664
return plot_backend .boxplot_frame_groupby (
482
665
grouped ,
483
666
subplots = subplots ,
@@ -584,6 +767,11 @@ class PlotAccessor(PandasObject):
584
767
labels with "(right)" in the legend.
585
768
include_bool : bool, default is False
586
769
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``.
587
775
**kwargs
588
776
Options to pass to matplotlib plotting method.
589
777
0 commit comments