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!

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 Overlaid Area Chart

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'
)
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[2]:

Overlaid Area Chart Without Boundary Lines

In [3]:
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[3]:

Interior Filling for Area Chart

In [4]:
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[4]:

Stacked Area Chart

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

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

trace0 = dict(
    x=x,
    y=[40, 60, 40, 10],
    hoverinfo='x+y',
    mode='lines',
    line=dict(width=0.5,
              color='rgb(131, 90, 241)'),
    stackgroup='one'
)
trace1 = dict(
    x=x,
    y=[20, 10, 10, 60],
    hoverinfo='x+y',
    mode='lines',
    line=dict(width=0.5,
              color='rgb(111, 231, 219)'),
    stackgroup='one'
)
trace2 = dict(
    x=x,
    y=[40, 30, 50, 30],
    hoverinfo='x+y',
    mode='lines',
    line=dict(width=0.5,
              color='rgb(184, 247, 212)'),
    stackgroup='one'
)
data = [trace0, trace1, trace2]

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

Stacked Area Chart with Normalized Values

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

trace0 = dict(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['40', '20', '30', '40'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(184, 247, 212)'),
    stackgroup='one',
    groupnorm='percent'
)
trace1 = dict(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['50', '70', '40', '60'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(111, 231, 219)'),
    stackgroup='one'
)
trace2 = dict(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['70', '80', '60', '70'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(127, 166, 238)'),
    stackgroup='one'
)
trace3 = dict(
    x=['Winter', 'Spring', 'Summer', 'Fall'],
    y=['100', '100', '100', '100'],
    mode='lines',
    line=dict(width=0.5,
              color='rgb(131, 90, 241)'),
    stackgroup='one'
)
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 = dict(data=data, layout=layout)
py.iplot(fig, filename='stacked-area-plot-norm', validate=False)
Out[6]:

Select Hover Points

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

trace0 = go.Scatter(
    x=[0,0.5,1,1.5,2],
    y=[0,1,2,1,0],
    fill= 'toself',
    fillcolor = '#ab63fa',
    hoveron = 'points+fills',
    line = dict(
      color = '#ab63fa'
    ),
    text = "Points + Fills",
    hoverinfo = 'text'
)

trace1 = go.Scatter(
    x=[3,3.5,4,4.5,5],
    y=[0,1,2,1,0],
    fill='toself',
    fillcolor = '#e763fa',
    hoveron = 'points',
    line = dict(
      color = '#e763fa'
    ),
    text = "Points only",
    hoverinfo = 'text'
)

data = [trace0, trace1]

layout = go.Layout(
    title = "hover on <i>points</i> or <i>fill</i>",
    xaxis = dict(
      range = [0,5.2]
    ),
    yaxis = dict(
      range = [0,3]
    )
)

fig = go.Figure(data=data,layout=layout)
py.iplot(data, filename='select-hover-points')
Out[7]:

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.