Examples
Using df.boxplot() Boxplots can be created for every column in the dataframe:
import numpy as np
import pandas as pd
np.random.seed(2345)
df = pd.DataFrame(np.random.randn(20,4),
columns=['C1', 'C2', 'C3', 'C4'])
boxplot = df.boxplot(column=['C1', 'C2', 'C3'])
Boxplots of variables distributions grouped by the values of a third variable:
df = pd.DataFrame(np.random.randn(20, 2),
columns=['C1', 'C2'])
df['X'] = pd.Series(['P', 'P', 'P', 'P', 'P',
'Q', 'Q', 'Q', 'Q', 'Q'])
boxplot = df.boxplot(by='X')
A list of strings (i.e. ['X', 'Y']) can be passed to boxplot in order to group the data
by combination of the variables in the x-axis:
df = pd.DataFrame(np.random.randn(20,3),
columns=['C1', 'C2', 'C3'])
df['X'] = pd.Series(['P', 'P', 'P', 'P', 'P',
'Q', 'Q', 'Q', 'Q', 'Q'])
df['Y'] = pd.Series(['P', 'P', 'P', 'P', 'P',
'Q', 'P', 'Q', 'P', 'Q'])
boxplot = df.boxplot(column=['C1', 'C2'], by=['X', 'Y'])
The layout of boxplot can be adjusted giving a tuple to layout:
boxplot = df.boxplot(column=['C1', 'C2'], by='X',
layout=(2, 1))
Additional formatting can be done to the boxplot:
boxplot = df.boxplot(grid=False, rot=55, fontsize=25)
The parameter return_type can be used to select the type of element returned by boxplot.
When return_type='axes' is selected, the matplotlib axes on which the boxplot is drawn are returned:
boxplot = df.boxplot(column=['C1','C2'], return_type='axes')
type(boxplot)
When grouping with by, a Series mapping columns to return_type is returned:
boxplot = df.boxplot(column=['C1', 'C2'], by='X',
return_type='axes')
type(boxplot)
If return_type is None, a NumPy array of axes with the same shape
as layout is returned:
boxplot = df.boxplot(column=['C1', 'C2'], by='X',
return_type=None)
type(boxplot)