Show Sidebar Hide Sidebar

Legends in Python

How to configure and style the legend in Plotly with Python.

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 API is updated frequently. Run pip install plotly --upgrade to update your Plotly version.

In [3]:
import plotly
plotly.__version__
Out[3]:
'2.0.6'

Show Legend

By default the legend is displayed on Plotly charts with multiple traces.

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
)

data = [trace0, trace1]
fig = go.Figure(data=data)

py.iplot(fig, filename='default-legend')
Out[4]:

Add showlegend=True to the layout object to display the legend on a plot with a single trace.

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
)

data = [trace0]
layout = go.Layout(showlegend=True)
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='show-legend')
Out[5]:

Hide Legend

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
)

data = [trace0, trace1]
layout = go.Layout(showlegend=False)
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='hide-legend')
Out[6]:

Hide Legend Entries

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
    showlegend=False
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
)

data = [trace0, trace1]
fig = go.Figure(data=data)

py.iplot(fig, filename='hide-legend-entry')
Out[7]:

Legend Names

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
    name='Positive'
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    name='Negative'
)

data = [trace0, trace1]
fig = go.Figure(data=data)

py.iplot(fig, filename='legend-names')
Out[8]:

Horizontal Legend

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
)

data = [trace0, trace1]
layout = go.Layout(
    legend=dict(orientation="h")
)
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='horizontal-legend')
Out[9]:

Legend Position

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
)

data = [trace0, trace1]
layout = go.Layout(
    legend=dict(x=-.1, y=1.2)
)
fig = go.Figure(data=data, layout=layout)

py.iplot(fig, filename='position-legend')
Out[10]:

Style Legend

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

trace0 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
)

trace1 = go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
)
data = [trace0, trace1]

layout = go.Layout(
    legend=dict(
        x=0,
        y=1,
        traceorder='normal',
        font=dict(
            family='sans-serif',
            size=12,
            color='#000'
        ),
        bgcolor='#E2E2E2',
        bordercolor='#FFFFFF',
        borderwidth=2
    )
)

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='style-legend')
Out[11]:

Grouped Legend

In [12]:
import plotly.plotly as py

data = [
    {
        'x': [1, 2, 3],
        'y': [2, 1, 3],
        'legendgroup': 'group', # this can be any string, not just "group"
        'name': 'first legend group',
        'mode': 'markers',
        'marker': {
            'color': 'rgb(164, 194, 244)'
        }
    },
    {
        'x': [1, 2, 3],
        'y': [2, 2, 2],
        'legendgroup': 'group',
        'name': 'first legend group - average',
        'mode': 'lines',
        'line': {
            'color': 'rgb(164, 194, 244)'
        }
    },   
    {
        'x': [1, 2, 3],
        'y': [4, 9, 2],
        'legendgroup': 'group2', 
        'name': 'second legend group',
        'mode': 'markers',
        'marker': {
            'color': 'rgb(142, 124, 195)'
        }
    },        
    {
        'x': [1, 2, 3],
        'y': [5, 5, 5],
        'legendgroup': 'group2',
        'name': 'second legend group - average',
        'mode': 'lines',
        'line': {
            'color': 'rgb(142, 124, 195)'
        }
    }
]

py.iplot(data, filename='basic-legend-grouping')
Out[12]:

You can also hide entries in grouped legends:

In [14]:
import plotly.plotly as py

data = [
    {
        'x': [1, 2, 3],
        'y': [2, 1, 3],
        'legendgroup': 'group',
        'name': 'first legend group',
        'mode': 'markers',
        'marker': {
            'color': 'rgb(164, 194, 244)'
        }
    },
    {
        'x': [1, 2, 3],
        'y': [2, 2, 2],
        'legendgroup': 'group',
        'name': 'first legend group - average',
        'mode': 'lines',
        'line': {
            'color': 'rgb(164, 194, 244)'
        },
        'showlegend': False
    },
    {
        'x': [1, 2, 3],
        'y': [4, 9, 2],
        'legendgroup': 'group2',
        'name': 'second legend group',
        'mode': 'markers',
        'marker': {
            'color': 'rgb(142, 124, 195)'
        }
    },
    {
        'x': [1, 2, 3],
        'y': [5, 5, 5],
        'legendgroup': 'group2',
        'name': 'second legend group - average',
        'mode': 'lines',
        'line': {
            'color': 'rgb(142, 124, 195)'
        },
        'showlegend': False
    }
]

py.iplot(data, filename='hiding-entries-from-grouped-legends')
Out[14]:
Still need help?
Contact Us

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