Show Sidebar Hide Sidebar

Box Plots in matplotlib

How to make a box plot in matplotlib. Examples of box plots in matplotlib that are grouped, colored, and display the underlying data distribution.

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.1.1'

Basic Box Plot

In [2]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low), 0)

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

ax.boxplot(data)

ax.set_xlabel('Data Points')
ax.set_ylabel('Variance')

plotly_fig = tls.mpl_to_plotly( mpl_fig )
py.iplot(plotly_fig, filename='boxplot-basic')
Out[2]:

Multiple Box Plots

In [3]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

# Generating sample data
N = 1000
data_set1 = np.random.normal(1, 1, N)
data_set2 = np.random.lognormal(1, 1, N)
data_set3 = np.random.exponential(1, N)
data_set4 = np.random.gumbel(6, 4, N)
data_set5 = np.random.triangular(2, 9, 11, N)

data = [data_set1, data_set2, data_set3, data_set4, data_set5]

mpl_fig = plt.figure()
ax = mpl_fig.add_subplot(111)

ax.boxplot(data)

plotly_fig = tls.mpl_to_plotly( mpl_fig )
py.iplot(plotly_fig, filename='mpl-multiple-boxplot')
Out[3]:

Box Plot with Outliers

In [4]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low), 0)

# change outlier point symbols
mpl_fig = plt.figure()
plt.boxplot(data, 0, 'gD')

plotly_fig = tls.mpl_to_plotly(mpl_fig)
py.iplot(plotly_fig, filename='mpl-boxplot-outliers')
Out[4]:

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.