Plotly图例标题

问题描述 投票:13回答:3

我想可以添加标题的传说,在下面的代码。然而,看着docs,我不认为有这种方法。

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')
python plotly
3个回答
11
投票

更新:

对于没有定义的传说,但有标注定位属性,请使用下面的代码。

import plotly.offline as py_offline
import plotly.graph_objs as go
py_offline.init_notebook_mode()

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(
    annotations=[
        dict(
            x=1.12,
            y=1.05,
            align="right",
            valign="top",
            text='Legend Title',
            showarrow=False,
            xref="paper",
            yref="paper",
            xanchor="center",
            yanchor="top"
        )
    ]
)
fig = go.Figure(data=data, layout = layout)

py_offline.iplot(fig)

笔记:

  1. 您需要定义xy位置使用这种方法的注释,对于不同的传说。
  2. 您可以使用HTML的text属性中(延续:text='Legend Title<br>kinda lengthy',

以前的尝试:

另一种方法是创造传奇和使用标注添加标题传说。只要你不编辑模式下使用图形。因此,在下面的例子中,说明被设定为x = 0且y = 1时,因为我希望我的图例标题为高于我的实际说明,我设置注释位置为x = 0,Y = 1.5。 X-REF和y REF需要被设置到纸上。这将提供一个很好的注解像plotly legend title

码:

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
    ),
    annotations=[
        dict(
            x=0,
            y=1.05,
            xref='paper',
            yref='paper',
            text='Legend Title',
            showarrow=False
        )
    ]
)
fig = go.Figure(data=data, layout = layout)

py.iplot(fig)

7
投票

我已经作出了无数据跟踪之前做到了这一点

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

dummy_trace = go.Scatter(
    x=[None], y=[None],
    name='<b>Legend Heading</b>',
    # set opacity = 0
    line={'color': 'rgba(0, 0, 0, 0)'}
)

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 = [dummy_trace, trace0, trace1]
fig = go.Figure(data=data)

py.iplot(fig)

2
投票

只是略有增加财产name到已经提出的解决方案,

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)

name属性有助于在添加自定义名称来定义的传说。

© www.soinside.com 2019 - 2024. All rights reserved.