Show Sidebar Hide Sidebar

Filled Area Plots in matplotlib

How to make a filled area plot in matplotlib. An area chart displays a solid color between the traces of a graph.

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'

Area Plot

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

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([2,1,3,1,2])

update = {'data':[{'fill': 'tozeroy'}]}  # this updates the trace
plotly_fig = tls.mpl_to_plotly( fig )
plotly_fig.update(update)
py.iplot(plotly_fig, update=update, filename='mpl-basic-area')
Out[2]:

Multiple Line Area Plot

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

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 100)
fig, ax = plt.subplots()
ax.plot(np.sin(x), label='sin'); ax.plot(np.cos(x), label='cos')

update = {'data':[{'fill': 'tozeroy'}]}  # this updates BOTH traces now
plotly_fig = tls.mpl_to_plotly( fig )
plotly_fig.update(update)
py.iplot(plotly_fig, filename='mpl-multi-fill')
Out[3]:

Stacked Line Plot

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

import numpy as np
import matplotlib.pyplot as plt

# create our stacked data manually
y0 = np.random.rand(100)
y1 = y0 + np.random.rand(100)
y2 = y1 + np.random.rand(100)
capacity = 3*np.ones(100)

# make the mpl plot (no fill yet)
fig, ax = plt.subplots()
ax.plot(y0, label='y0')
ax.plot(y1, label='y1')
ax.plot(y2, label='y2')
ax.plot(capacity, label='capacity')

# set all traces' "fill" so that it fills to the next 'y' trace
update = {'data':[{'fill': 'tonexty'}]}

# strip style just lets Plotly make the styling choices (e.g., colors)
plotly_fig = tls.mpl_to_plotly( fig )
plotly_fig.update(update)
py.iplot(plotly_fig, strip_style=True, filename='mpl-stacked-line')
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.