#!/usr/bin/env python
# coding: utf-8
# [Sebastian Raschka](http://www.sebastianraschka.com)
#
# [back](https://github.com/rasbt/matplotlib-gallery) to the `matplotlib-gallery` at [https://github.com/rasbt/matplotlib-gallery](https://github.com/rasbt/matplotlib-gallery)
# In[1]:
get_ipython().run_line_magic('load_ext', 'watermark')
# In[2]:
get_ipython().run_line_magic('watermark', '-u -v -d -p matplotlib,numpy')
# [More info](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/ipython_magic/watermark.ipynb) about the `%watermark` extension
# In[3]:
get_ipython().run_line_magic('matplotlib', 'inline')
#
#
# # Boxplots in matplotlib
# # Sections
# - [Simple Boxplot](#Simple-Boxplot)
#
# - [Black and white Boxplot](#Black-and-white-Boxplot)
#
# - [Horizontal Boxplot](#Horizontal-Boxplot)
#
# - [Filled and cylindrical boxplots](#Filled-and-cylindrical-boxplots)
#
# - [Boxplots with custom fill colors](#Boxplots-with-custom-fill-colors)
#
# - [Violin plots](#Violin-plots)
#
#
#
#
# 
#
#
# # Simple Boxplot
# [[back to top](#Sections)]
# In[7]:
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
fig = plt.figure(figsize=(8,6))
plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()
#
#
# # Black and white Boxplot
# [[back to top](#Sections)]
# In[8]:
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
fig = plt.figure(figsize=(8,6))
bplot = plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=True) # vertical box aligmnent
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
for components in bplot.keys():
for line in bplot[components]:
line.set_color('black') # black lines
t = plt.title('Black and white box plot')
plt.show()
#
#
# # Horizontal Boxplot
# [[back to top](#Sections)]
# In[13]:
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
fig = plt.figure(figsize=(8,6))
plt.boxplot(all_data,
notch=False, # box instead of notch shape
sym='rs', # red squares for outliers
vert=False) # horizontal box aligmnent
plt.yticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.ylabel('measurement x')
t = plt.title('Horizontal Box plot')
plt.show()
#
#
# # Filled and cylindrical boxplots
# [[back to top](#Sections)]
# In[10]:
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
fig = plt.figure(figsize=(8,6))
plt.boxplot(all_data,
notch=True, # notch shape
sym='bs', # blue squares for outliers
vert=True, # vertical box aligmnent
patch_artist=True) # fill with color
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()
#
#
# # Boxplots with custom fill colors
# [[back to top](#Sections)]
# In[31]:
import matplotlib.pyplot as plt
import numpy as np
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
fig = plt.figure(figsize=(8,6))
bplot = plt.boxplot(all_data,
notch=False, # notch shape
vert=True, # vertical box aligmnent
patch_artist=True) # fill with color
colors = ['pink', 'lightblue', 'lightgreen']
for patch, color in zip(bplot['boxes'], colors):
patch.set_facecolor(color)
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()
#
#
# # Violin plots
# [[back to top](#Sections)]
# Violin plots are closely related to Tukey's (1977) box plots but add useful information such as the distribution of the sample data (density trace).
#
# Violin plots were added in [matplotlib 1.4](http://matplotlib.org/1.4.0/users/whats_new.html).
# In[9]:
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(12,5))
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
#fig = plt.figure(figsize=(8,6))
axes[0].violinplot(all_data,
showmeans=False,
showmedians=True
)
axes[0].set_title('violin plot')
axes[1].boxplot(all_data,
)
axes[1].set_title('box plot')
# adding horizontal grid lines
for ax in axes:
ax.yaxis.grid(True)
ax.set_xticks([y+1 for y in range(len(all_data))], )
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
plt.setp(axes, xticks=[y+1 for y in range(len(all_data))],
xticklabels=['x1', 'x2', 'x3', 'x4'],
)
plt.show()
# In[ ]:
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: