Show Sidebar Hide Sidebar

Filled Area Plots in Python

How to make filled area plots in Python with Plotly.

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!

Basic Overlaid Area Chart

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[0, 2, 3, 5],
    fill='tozeroy'
)
trace2 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[3, 5, 1, 7],
    fill='tonexty'
)

data = [trace1, trace2]
py.iplot(data, filename='basic-area')
Out[1]:

Overlaid Area Chart Without Boundary Lines

In [2]:
import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[0, 2, 3, 5],
    fill='tozeroy',
    mode= 'none'
)
trace2 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[3, 5, 1, 7],
    fill='tonexty',
    mode= 'none'
)

data = [trace1, trace2]
py.iplot(data, filename='basic-area-no-bound')
Out[2]:

Interior Filling for Area Chart

In [3]:
import plotly.plotly as py
import plotly.graph_objs as go

trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[3, 4, 8, 3],
    fill= None,
    mode='lines',
    line=dict(
        color='rgb(143, 19, 131)',
    )
)
trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[1, 6, 2, 6],
    fill='tonexty',
    mode='lines',
    line=dict(
        color='rgb(143, 19, 131)',
    )
)

data = [trace0, trace1]
py.iplot(data, filename='filling-interior-area')
Out[3]:

Stacked Area Chart with Cumulative Values

In [4]:
import plotly.plotly as py
import plotly.graph_objs as go

trace0 = go.Scatter(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['40', '20', '30', '40'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(184, 247, 212)'),
    fill='tonexty'
)
trace1 = go.Scatter(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['50', '70', '40', '60'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(111, 231, 219)'),
    fill='tonexty'
)
trace2 = go.Scatter(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['70', '80', '60', '70'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(127, 166, 238)'),
    fill='tonexty'
)
trace3 = go.Scatter(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['100', '100', '100', '100'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(131, 90, 241)'),
    fill='tonexty'
)
data = [trace0, trace1, trace2, trace3]
layout = go.Layout(
    showlegend=True,
    xaxis=dict(
        type='category',
    ),
    yaxis=dict(
        type='linear',
        range=[1, 100],
        dtick=20,
        ticksuffix='%'
    )
)
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='stacked-area-plot')
Out[4]:

Stacked Area Chart with Original Values

In [5]:
import plotly.plotly as py
import plotly.graph_objs as go

# Add original data
x=['Winter', 'Spring', 'Summer', 'Fall']

y0_org=[40, 60, 40, 10]
y1_org=[20, 10, 10, 60]
y2_org=[40, 30, 50, 30]

# Add data to create cumulative stacked values
y0_stck=y0_org
y1_stck=[y0+y1 for y0, y1 in zip(y0_org, y1_org)]
y2_stck=[y0+y1+y2 for y0, y1, y2 in zip(y0_org, y1_org, y2_org)]

# Make original values strings and add % for hover text
y0_txt=[str(y0)+'%' for y0 in y0_org]
y1_txt=[str(y1)+'%' for y1 in y1_org]
y2_txt=[str(y2)+'%' for y2 in y2_org]

trace0 = go.Scatter(
    x=x,
    y=y0_stck,
    text=y0_txt,
    hoverinfo='x+text',
    mode='lines',
    line=dict(width=0.5,
              color='rgb(131, 90, 241)'),
    fill='tonexty'
)
trace1 = go.Scatter(
    x=x,
    y=y1_stck,
    text=y1_txt,
    hoverinfo='x+text',
    mode='lines',
    line=dict(width=0.5,
              color='rgb(111, 231, 219)'),
    fill='tonexty'
)
trace2 = go.Scatter(
    x=x,
    y=y2_stck,
    text=y2_txt,
    hoverinfo='x+text',
    mode='lines',
    line=dict(width=0.5,
              color='rgb(184, 247, 212)'),
    fill='tonexty'
)
data = [trace0, trace1, trace2]

fig = go.Figure(data=data)
py.iplot(fig, filename='stacked-area-plot-hover')
Out[5]:

Reference

See https://plot.ly/python/reference/#scatter-line and https://plot.ly/python/reference/#scatter-fill for more information and attribute options!

Still need help?
Contact Us

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