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.

Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.
The version 4 version of this page is here.

#### 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 :
import plotly
plotly.__version__

Out:
'3.1.1'

#### Basic Box Plot¶

In :
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:

#### Multiple Box Plots¶

In :
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:

#### Box Plot with Outliers¶

In :
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:

#### Reference¶

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