Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.boxplot#

DataFrame.boxplot(column=None,by=None,ax=None,fontsize=None,rot=0,grid=True,figsize=None,layout=None,return_type=None,backend=None,**kwargs)[source]#

Make a box plot from DataFrame columns.

Make a box-and-whisker plot from DataFrame columns, optionally groupedby some other columns. A box plot is a method for graphically depictinggroups of numerical data through their quartiles.The box extends from the Q1 to Q3 quartile values of the data,with a line at the median (Q2). The whiskers extend from the edgesof box to show the range of the data. By default, they extend no more than1.5 * IQR (IQR = Q3 - Q1) from the edges of the box, ending at the farthestdata point within that interval. Outliers are plotted as separate dots.

For further details seeWikipedia’s entry forboxplot.

Parameters:
columnstr or list of str, optional

Column name or list of names, or vector.Can be any valid input topandas.DataFrame.groupby().

bystr or array-like, optional

Column in the DataFrame topandas.DataFrame.groupby().One box-plot will be done per value of columns inby.

axobject of class matplotlib.axes.Axes, optional

The matplotlib axes to be used by boxplot.

fontsizefloat or str

Tick label font size in points or as a string (e.g.,large).

rotfloat, default 0

The rotation angle of labels (in degrees)with respect to the screen coordinate system.

gridbool, default True

Setting this to True will show the grid.

figsizeA tuple (width, height) in inches

The size of the figure to create in matplotlib.

layouttuple (rows, columns), optional

For example, (3, 5) will display the subplotsusing 3 rows and 5 columns, starting from the top-left.

return_type{‘axes’, ‘dict’, ‘both’} or None, default ‘axes’

The kind of object to return. The default isaxes.

  • ‘axes’ returns the matplotlib axes the boxplot is drawn on.

  • ‘dict’ returns a dictionary whose values are the matplotlibLines of the boxplot.

  • ‘both’ returns a namedtuple with the axes and dict.

  • when grouping withby, a Series mapping columns toreturn_type is returned.

    Ifreturn_type isNone, a NumPy arrayof axes with the same shape aslayout is returned.

backendstr, default None

Backend to use instead of the backend specified in the optionplotting.backend. For instance, ‘matplotlib’. Alternatively, tospecify theplotting.backend for the whole session, setpd.options.plotting.backend.

**kwargs

All other plotting keyword arguments to be passed tomatplotlib.pyplot.boxplot().

Returns:
result

See Notes.

See also

pandas.Series.plot.hist

Make a histogram.

matplotlib.pyplot.boxplot

Matplotlib equivalent plot.

Notes

The return type depends on thereturn_type parameter:

  • ‘axes’ : object of class matplotlib.axes.Axes

  • ‘dict’ : dict of matplotlib.lines.Line2D objects

  • ‘both’ : a namedtuple with structure (ax, lines)

For data grouped withby, return a Series of the above or a numpyarray:

  • Series

  • array (forreturn_type=None)

Usereturn_type='dict' when you want to tweak the appearanceof the lines after plotting. In this case a dict containing the Linesmaking up the boxes, caps, fliers, medians, and whiskers is returned.

Examples

Boxplots can be created for every column in the dataframebydf.boxplot() or indicating the columns to be used:

>>>np.random.seed(1234)>>>df=pd.DataFrame(np.random.randn(10,4),...columns=['Col1','Col2','Col3','Col4'])>>>boxplot=df.boxplot(column=['Col1','Col2','Col3'])
../../_images/pandas-DataFrame-boxplot-1.png

Boxplots of variables distributions grouped by the values of a thirdvariable can be created using the optionby. For instance:

>>>df=pd.DataFrame(np.random.randn(10,2),...columns=['Col1','Col2'])>>>df['X']=pd.Series(['A','A','A','A','A',...'B','B','B','B','B'])>>>boxplot=df.boxplot(by='X')
../../_images/pandas-DataFrame-boxplot-2.png

A list of strings (i.e.['X','Y']) can be passed to boxplotin order to group the data by combination of the variables in the x-axis:

>>>df=pd.DataFrame(np.random.randn(10,3),...columns=['Col1','Col2','Col3'])>>>df['X']=pd.Series(['A','A','A','A','A',...'B','B','B','B','B'])>>>df['Y']=pd.Series(['A','B','A','B','A',...'B','A','B','A','B'])>>>boxplot=df.boxplot(column=['Col1','Col2'],by=['X','Y'])
../../_images/pandas-DataFrame-boxplot-3.png

The layout of boxplot can be adjusted giving a tuple tolayout:

>>>boxplot=df.boxplot(column=['Col1','Col2'],by='X',...layout=(2,1))
../../_images/pandas-DataFrame-boxplot-4.png

Additional formatting can be done to the boxplot, like suppressing the grid(grid=False), rotating the labels in the x-axis (i.e.rot=45)or changing the fontsize (i.e.fontsize=15):

>>>boxplot=df.boxplot(grid=False,rot=45,fontsize=15)
../../_images/pandas-DataFrame-boxplot-5.png

The parameterreturn_type can be used to select the type of elementreturned byboxplot. Whenreturn_type='axes' is selected,the matplotlib axes on which the boxplot is drawn are returned:

>>>boxplot=df.boxplot(column=['Col1','Col2'],return_type='axes')>>>type(boxplot)<class 'matplotlib.axes._axes.Axes'>

When grouping withby, a Series mapping columns toreturn_typeis returned:

>>>boxplot=df.boxplot(column=['Col1','Col2'],by='X',...return_type='axes')>>>type(boxplot)<class 'pandas.core.series.Series'>

Ifreturn_type isNone, a NumPy array of axes with the same shapeaslayout is returned:

>>>boxplot=df.boxplot(column=['Col1','Col2'],by='X',...return_type=None)>>>type(boxplot)<class 'numpy.ndarray'>

[8]ページ先頭

©2009-2025 Movatter.jp