Plotly legend title

前端 未结 4 687
既然无缘
既然无缘 2021-01-07 18:02

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 plo         


        
4条回答
  •  死守一世寂寞
    2021-01-07 19:02

    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.

提交回复
热议问题