Skip to content

Commit c79bfd2

Browse files
committed
add inline comments to plotting.boxplot_frame
1 parent f8d4cb5 commit c79bfd2

File tree

1 file changed

+170
-2
lines changed

1 file changed

+170
-2
lines changed

pandas/plotting/_core.py

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,6 @@ def boxplot(
684684
)
685685

686686

687-
@Substitution(data="", backend=_backend_doc)
688-
@Appender(_boxplot_doc)
689687
def boxplot_frame(
690688
self: DataFrame,
691689
column=None,
@@ -700,6 +698,176 @@ def boxplot_frame(
700698
backend=None,
701699
**kwargs,
702700
):
701+
"""
702+
703+
Make a box plot from DataFrame columns.
704+
705+
Make a box-and-whisker plot from DataFrame columns, optionally grouped
706+
by some other columns. A box plot is a method for graphically depicting
707+
groups of numerical data through their quartiles.
708+
The box extends from the Q1 to Q3 quartile values of the data,
709+
with a line at the median (Q2). The whiskers extend from the edges
710+
of box to show the range of the data. By default, they extend no more than
711+
`1.5 * IQR (IQR = Q3 - Q1)` from the edges of the box, ending at the farthest
712+
data point within that interval. Outliers are plotted as separate dots.
713+
714+
For further details see
715+
Wikipedia's entry for `boxplot <https://en.wikipedia.org/wiki/Box_plot>`_.
716+
717+
Parameters
718+
----------
719+
column : str or list of str, optional
720+
Column name or list of names, or vector.
721+
Can be any valid input to :meth:`pandas.DataFrame.groupby`.
722+
by : str or array-like, optional
723+
Column in the DataFrame to :meth:`pandas.DataFrame.groupby`.
724+
One box-plot will be done per value of columns in `by`.
725+
ax : object of class matplotlib.axes.Axes, optional
726+
The matplotlib axes to be used by boxplot.
727+
fontsize : float or str
728+
Tick label font size in points or as a string (e.g., `large`).
729+
rot : float, default 0
730+
The rotation angle of labels (in degrees)
731+
with respect to the screen coordinate system.
732+
grid : bool, default True
733+
Setting this to True will show the grid.
734+
figsize : A tuple (width, height) in inches
735+
The size of the figure to create in matplotlib.
736+
layout : tuple (rows, columns), optional
737+
For example, (3, 5) will display the subplots
738+
using 3 rows and 5 columns, starting from the top-left.
739+
return_type : {'axes', 'dict', 'both'} or None, default 'axes'
740+
The kind of object to return. The default is ``axes``.
741+
742+
* 'axes' returns the matplotlib axes the boxplot is drawn on.
743+
* 'dict' returns a dictionary whose values are the matplotlib
744+
Lines of the boxplot.
745+
* 'both' returns a namedtuple with the axes and dict.
746+
* when grouping with ``by``, a Series mapping columns to
747+
``return_type`` is returned.
748+
749+
If ``return_type`` is `None`, a NumPy array
750+
of axes with the same shape as ``layout`` is returned.
751+
backend : str, default None
752+
Backend to use instead of the backend specified in the option
753+
``plotting.backend``. For instance, 'matplotlib'. Alternatively, to
754+
specify the ``plotting.backend`` for the whole session, set
755+
``pd.options.plotting.backend``.
756+
757+
**kwargs
758+
All other plotting keyword arguments to be passed to
759+
:func:`matplotlib.pyplot.boxplot`.
760+
761+
Returns
762+
-------
763+
result
764+
See Notes.
765+
766+
See Also
767+
--------
768+
Series.plot.hist: Make a histogram.
769+
matplotlib.pyplot.boxplot : Matplotlib equivalent plot.
770+
771+
Notes
772+
-----
773+
The return type depends on the `return_type` parameter:
774+
775+
* 'axes' : object of class matplotlib.axes.Axes
776+
* 'dict' : dict of matplotlib.lines.Line2D objects
777+
* 'both' : a namedtuple with structure (ax, lines)
778+
779+
For data grouped with ``by``, return a Series of the above or a numpy
780+
array:
781+
782+
* :class:`~pandas.Series`
783+
* :class:`~numpy.array` (for ``return_type = None``)
784+
785+
Use ``return_type='dict'`` when you want to tweak the appearance
786+
of the lines after plotting. In this case a dict containing the Lines
787+
making up the boxes, caps, fliers, medians, and whiskers is returned.
788+
789+
Examples
790+
--------
791+
792+
Boxplots can be created for every column in the dataframe
793+
by ``df.boxplot()`` or indicating the columns to be used:
794+
795+
.. plot::
796+
:context: close-figs
797+
798+
>>> np.random.seed(1234)
799+
>>> df = pd.DataFrame(np.random.randn(10, 4),
800+
... columns=['Col1', 'Col2', 'Col3', 'Col4'])
801+
>>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3']) # doctest: +SKIP
802+
803+
Boxplots of variables distributions grouped by the values of a third
804+
variable can be created using the option ``by``. For instance:
805+
806+
.. plot::
807+
:context: close-figs
808+
809+
>>> df = pd.DataFrame(np.random.randn(10, 2),
810+
... columns=['Col1', 'Col2'])
811+
>>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
812+
... 'B', 'B', 'B', 'B', 'B'])
813+
>>> boxplot = df.boxplot(by='X')
814+
815+
A list of strings (i.e. ``['X', 'Y']``) can be passed to boxplot
816+
in order to group the data by combination of the variables in the x-axis:
817+
818+
.. plot::
819+
:context: close-figs
820+
821+
>>> df = pd.DataFrame(np.random.randn(10, 3),
822+
... columns=['Col1', 'Col2', 'Col3'])
823+
>>> df['X'] = pd.Series(['A', 'A', 'A', 'A', 'A',
824+
... 'B', 'B', 'B', 'B', 'B'])
825+
>>> df['Y'] = pd.Series(['A', 'B', 'A', 'B', 'A',
826+
... 'B', 'A', 'B', 'A', 'B'])
827+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by=['X', 'Y'])
828+
829+
The layout of boxplot can be adjusted giving a tuple to ``layout``:
830+
831+
.. plot::
832+
:context: close-figs
833+
834+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
835+
... layout=(2, 1))
836+
837+
Additional formatting can be done to the boxplot, like suppressing the grid
838+
(``grid=False``), rotating the labels in the x-axis (i.e. ``rot=45``)
839+
or changing the fontsize (i.e. ``fontsize=15``):
840+
841+
.. plot::
842+
:context: close-figs
843+
844+
>>> boxplot = df.boxplot(grid=False, rot=45, fontsize=15) # doctest: +SKIP
845+
846+
The parameter ``return_type`` can be used to select the type of element
847+
returned by `boxplot`. When ``return_type='axes'`` is selected,
848+
the matplotlib axes on which the boxplot is drawn are returned:
849+
850+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], return_type='axes')
851+
>>> type(boxplot)
852+
<class 'matplotlib.axes._axes.Axes'>
853+
854+
When grouping with ``by``, a Series mapping columns to ``return_type``
855+
is returned:
856+
857+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
858+
... return_type='axes')
859+
>>> type(boxplot)
860+
<class 'pandas.Series'>
861+
862+
If ``return_type`` is `None`, a NumPy array of axes with the same shape
863+
as ``layout`` is returned:
864+
865+
>>> boxplot = df.boxplot(column=['Col1', 'Col2'], by='X',
866+
... return_type=None)
867+
>>> type(boxplot)
868+
<class 'numpy.ndarray'>
869+
"""
870+
703871
plot_backend = _get_plot_backend(backend)
704872
return plot_backend.boxplot_frame(
705873
self,

0 commit comments

Comments
 (0)