In [33]:
%matplotlib inline
## numpy is used for creating fake data
import numpy as np
import pandas as pd
import matplotlib as mpl
## agg backend is used to create plot as a .png file
# mpl.use('agg')
import matplotlib.pyplot as plt
In [24]:
## Create data
np.random.seed(10)
collectn_1 = np.random.normal(100, 10, 200)
collectn_2 = np.random.normal(80, 30, 200)
collectn_3 = np.random.normal(90, 20, 200)
collectn_4 = np.random.normal(70, 25, 200)
## combine these different collections into a list
data_to_plot = [collectn_1, collectn_2, collectn_3, collectn_4]
In [25]:
# Create a figure instance
fig = plt.figure(1, figsize=(9, 6))
# Create an axes instance
ax = fig.add_subplot(111)
# Create the boxplot
bp = ax.boxplot(data_to_plot)
In [27]:
## change outline color, fill color and linewidth of the boxes
for box in bp['boxes']:
# change outline color
box.set( color='#7570b3', linewidth=2)
# change fill color
box.set( color = '#1b9e77' )
## change color and linewidth of the whiskers
for whisker in bp['whiskers']:
whisker.set(color='#7570b3', linewidth=2)
## change color and linewidth of the caps
for cap in bp['caps']:
cap.set(color='#7570b3', linewidth=2)
## change color and linewidth of the medians
for median in bp['medians']:
median.set(color='#b2df8a', linewidth=2)
## change the style of fliers and their fill
for flier in bp['fliers']:
flier.set(marker='o', color='#e7298a', alpha=0.5)
In [28]:
## Custom x-axis labels
ax.set_xticklabels(['Sample1', 'Sample2', 'Sample3', 'Sample4'])
## Remove top axes and right axes ticks
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
In [19]:
# Save the figure
# fig.savefig('fig1.png', bbox_inches='tight')
In [35]:
df = pd.DataFrame(data_to_plot)
df.head()
Out[35]:
In [36]:
df = df.T
In [37]:
df.plot(kind='box')
Out[37]:
In [42]:
color = dict(boxes='DarkGreen', whiskers='DarkOrange',
medians='DarkBlue', caps='Gray')
df.columns = ['Sample1', 'Sample2', 'Sample3', 'Sample4']
df.plot(kind='box', color=color, sym='r+')
Out[42]:
In [43]:
df['Sample1'].plot('box')
Out[43]:
In [ ]:
df