Show Sidebar Hide Sidebar

Legends in Pandas

How to modify the legend in pandas graphs. Seven examples of how to move, color, and hide the legend.

# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 3, 6, 4, 5, 2, 3, 5, 4]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 4, 7, 8, 3, 6, 3, 3, 4]
)
data = [trace1, trace2]
layout = go.Layout(
    showlegend=False
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='legend-visibility')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 3, 6, 4, 5, 2, 3, 5, 4],
    name='Blue Trace'
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 4, 7, 8, 3, 6, 3, 3, 4],
    name='Orange Trace'
)
data = [trace1, trace2]
plot_url = py.plot(data, filename='legend-labels')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 3, 6, 4, 5, 2, 3, 5, 4]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 4, 7, 8, 3, 6, 3, 3, 4]
)
data = [trace1, trace2]
layout = go.Layout(
    showlegend=True,
    legend=dict(x=0.9, y=1)
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='legend-inside')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 3, 6, 4, 5, 2, 3, 5, 4]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 4, 7, 8, 3, 6, 3, 3, 4]
)
data = [trace1, trace2]
layout = go.Layout(
    showlegend=True,
    legend=dict(x=100, y=1)
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='legend-outside')
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 3, 6, 4, 5, 2, 3, 5, 4]
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 4, 7, 8, 3, 6, 3, 3, 4]
)
data = [trace1, trace2]
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)
plot_url = py.plot(fig, filename='legend-style')
import plotly.plotly as py

py.plot([
    {
        '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)'
        }
    }
], filename='basic-legend-grouping')
Plotly legends are interactive. Click on the legend entries to hide and show traces. The legendgroup key groups legend entries so that clicking on one legend entry will hide or show all of the traces in that group.
# Learn about API authentication here: https://plot.ly/pandas/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
import plotly.graph_objs as go

trace1 = go.Scatter(
    x=[0, 1, 2],
    y=[1, 2, 3],
    name='First Trace',
    showlegend=False
)
trace2 = go.Scatter(
    x=[0, 1, 2, 3],
    y=[8, 4, 2, 0],
    name='Second Trace',
    showlegend=True
)
data = [trace1, trace2]
plot_url = py.plot(data, filename='show-legend')
Still need help?
Contact Us

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