如何在 Plotly 的情节内定位图例

Leo*_*ime 6 python graph legend python-3.x plotly

我从 Plotly 页面得到了这段代码。我需要使背景透明并突出显示轴。还有位于情节内的传说。

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
    name="Increasing"
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    name="Decreasing"
))

fig.update_layout(legend_title='<b> Trend </b>')
fig.show()
Run Code Online (Sandbox Code Playgroud)

上面的代码显示了下面的输出:

在此处输入图片说明

我的预期输出:

在此处输入图片说明

如何转换第一张图像以获得第二张图像的特征?

小智 7

import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
))

fig.update_layout(
    legend=dict(
        x=0,
        y=.5,
        traceorder="normal",
        font=dict(
            family="sans-serif",
            size=12,
            color="black"
        ),
    )
)
fig.show()
Run Code Online (Sandbox Code Playgroud)

在 0 到 1 之间更改 x 和 y 的值

输出


sen*_*nce 6

要更改背景颜色,您需要通过 指定它plot_bgcolor='rgba(0,0,0,0)',,而要在图中移动图例,在左侧,您需要明确定义位置:

import plotly.graph_objects as go

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

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

data = [trace0, trace1]
layout = go.Layout(
        plot_bgcolor='rgba(0,0,0,0)',
    legend=dict(
        x=0,
        y=0.7,
        traceorder='normal',
        font=dict(
            size=12,),
    ),
    annotations=[
        dict(
            x=0,
            y=0.75,
            xref='paper',
            yref='paper',
            text='Trend',
            showarrow=False
        )
    ]
)
fig = go.Figure(data = data,
                layout = layout)

fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='LightGray')
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='LightGray')

fig.show()
Run Code Online (Sandbox Code Playgroud)

你会得到:

在此处输入图片说明