Plotly legend title

Viewed 21508

I'd like to be able to add a title to the legend, in the following code. However, looking at the docs, I don't think there is a method for this.

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')
7 Answers

Just a slight addition of property name to the already proposed solution,

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

plotly.offline.init_notebook_mode(connected=True)

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

data = [trace0]
layout = go.Layout(
legend=dict(
    x=0,
    y=1,
    traceorder='normal',
    font=dict(
        family='sans-serif',
        size=12,
        color='#000'
    ),
    bgcolor='#E2E2E1',
    bordercolor='#FFFFFF',
    borderwidth=2
),
annotations=[
    dict(
        x=0,
        y=1.05,
        xref='paper',
        yref='paper',
        text='Legend Title',
        showarrow=False
    )
])
fig = go.Figure(data=data, layout = layout)
plotly.offline.iplot(fig)

The name property helps in adding custom names to the legends defined.

As of plotly v4.5, legend titles have been added. You can add a title for a legend by fig.update_layout(legend_title_text='Legend title')

Find an example in the documentation here.

It is very easy to plotly with jupyter through wrapper cufflinks.

install these dependencies:

!pip install pandas cufflinks plotly

create data frame from your data.

import pandas as pd

load data

x=[1, 2, 3, 4, 5]
y=[1, 2, 3, 4, 5]

make second y list to y1

x=[1, 2, 3, 4, 5]
y1=[5, 4, 3, 2, 1]

create dataframe from your data list

data = pd.DataFrame({"x": x, "y": y, "y1": y1}).set_index("x")

load cufflinks and its configuration

import cufflinks as cf
cf.set_config_file(offline=True) 

plot dataframe

data.iplot()

You will x on axis, y, y1 on y axis. You can show and hide line clicking on legend from right side.

References:

Another (now probably moot) option for discrete data is using one of a dataframe's column headers as the legend title. Below an example for a choropleth map consisting of three (Geo)DataFrames.

df1['Legend'] = 'Label1'
df2['Legend'] = 'Label2'
df3['Legend'] = 'Label3'
merged = df1.append(df2)
merged = df2.append(df3)

fig = px.choropleth(merged, geojson=merged.geometry, locations=merged.index, color=merged['Legend'], color_discrete_map={
    'Label1':'red',
    'Label2':'lightgrey',
    'Label3':'lightblue'})

And here an example using a mapbox choropleth improvised choropleth map legend using column name

Currently with the last version of plotly 5.5.0, all you need:

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)

fig.update_layout(legend={"title":"Points"})  #<--- add only this line

fig.show()

enter image description here

Related