图文并茂的传说

2024-10-04 03:20:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望能够在下面的代码中为图例添加标题。然而,看看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')

Tags: 方法代码pyimportgo标题docsdata
3条回答

我以前做过这样的工作,减少了数据跟踪

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)

只是在已经提出的解决方案中添加了一些属性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属性有助于将自定义名称添加到定义的图例中。

更新:

若要不定义图例但具有批注定位属性,请使用以下代码。

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. 您可以在text属性中使用html(例如:text='Legend Title<br>kinda lengthy',

上一次尝试:

另一种方法是创建图例并使用注释将标题添加到图例中。如果不在可编辑模式下使用图形。因此在下面的例子中,图例设置为x=0和y=1,因为我希望图例标题位于实际图例之上,所以我将注释位置设置为x=0,y=1.5。x-ref和y-ref需要设置为paper。这将给出一个很好的注释,比如 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)

相关问题 更多 >