How Do I Create A Box Plot For Each Column In A Pandas Dataframe?
My data frames (pandas's structure) looks like above Now I want to make boxplot for each feature on separate canvas. The separation condition is the first column. I have similar p
Solution 1:
DataFrame.boxplot()
automates this rather well:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({'is_true_seed': np.random.choice([True, False], 10),
'col1': np.random.normal(size=10),
'col2': np.random.normal(size=10),
'col3': np.random.normal(size=10)})
is_true_seed col1 col2 col3
0False -0.990041 -0.561413 -0.5125821False0.8250990.827453 -0.3662112True0.083442 -1.1995400.3457923True0.0657151.560029 -0.3245014True -1.699770 -0.270820 -1.380125
ax = df.boxplot(['col1', 'col2', 'col3'], 'is_true_seed', figsize=(10, 10))
The first argument tells pandas which columns to plot, the second which column to group by (what you call the separation condition), and the third on which axes to draw.
Listing all columns but the one you want to group by can get tedious, but you can avoid it by omitting that first argument. You then have to explicitly name the other two:
ax = df.boxplot(by='is_true_seed', figsize=(10, 10))
Solution 2:
Linking my answer from another related question
If you want to create a separate plot per column, then you can iterate over each column and use plt.figure()
to initiate a new figure for each plot.
import matplotlib.pyplot as plt
for column in df:
plt.figure()
df.boxplot([column])
If you want to just put all columns into the same boxplot graph then you can just use df.plot(kind='box')
Post a Comment for "How Do I Create A Box Plot For Each Column In A Pandas Dataframe?"