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.

import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

# 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)
plot_url = py.plot_mpl(fig, update=update, strip_style=True, filename='mpl-stacked-line')
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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
plot_url = py.plot_mpl(fig, update=update, filename='mpl-multi-fill')
import matplotlib.pyplot as plt
import plotly.plotly as py
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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

update = {'data':[{'fill': 'tozeroy'}]}  # this updates the trace
plot_url = py.plot_mpl(fig, update=update, filename='mpl-basic-area')
Still need help?
Contact Us

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