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]:
0 1 2 3 4 5 6 7 8 9 ... 190 191 192 193 194 195 196 197 198 199
0 113.315865 107.152790 84.545997 99.916162 106.213360 92.799144 102.655116 101.085485 100.042914 98.253998 ... 103.534776 97.927205 89.203026 98.769302 96.090178 112.551737 109.471261 89.776893 111.671684 94.280232
1 83.994125 116.082316 49.257411 84.811975 46.085741 21.761010 109.597935 80.846821 55.328972 32.403447 ... 86.783790 76.523578 92.348072 88.634313 95.128144 104.128021 49.081754 39.269073 72.004944 74.861216
2 99.247711 65.602889 93.851457 98.709008 57.301117 98.698745 117.956388 92.493336 72.170972 78.462133 ... 33.679976 92.190889 92.853386 74.319289 95.152215 116.258455 63.019493 100.002737 70.308267 119.719588
3 84.267336 57.178120 76.894561 60.267961 86.213235 69.389287 102.712179 136.564437 78.568160 91.815754 ... 54.872401 77.098273 97.428021 56.535566 124.889919 52.433905 68.485011 75.430705 31.874143 81.447299

4 rows × 200 columns


In [36]:
df = df.T

In [37]:
df.plot(kind='box')


Out[37]:
<matplotlib.axes._subplots.AxesSubplot at 0x106937518>

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]:
<matplotlib.axes._subplots.AxesSubplot at 0x109737cc0>

In [43]:
df['Sample1'].plot('box')


Out[43]:
<matplotlib.axes._subplots.AxesSubplot at 0x10a251cc0>

In [ ]:
df