Plotly:如何更改图例中显示的线条的大小?

Sul*_*rif 5 python legend plotly plotly-python

我很难让传奇在情节上变得更大。我已经用尽了文档、论坛、github 问题,但一无所获。开始思考情节并不是什么伟大的软件。

我创建了这个图表:

在此输入图像描述

我想使试用版 1、试用版 2 和试用版 3 的图例中的线条变大。我可以使用他们的 api 将字体变大,但我没有看到对图例行的引用,现在想知道它是否可能。

这是一些代码:


fig.update_layout(
    title_text="Dipole Moment X (0 V/nm)",
    title_font=dict(size=44, family='Arial'),
    template='simple_white',
    xaxis_tickformat = 'i',
    bargap=0.2, # gap between bars of adjacent location coordinates,
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.02,
        xanchor="right",
        x=1,  
        font = dict(family = "Arial", size = 60),
        bordercolor="LightSteelBlue",
        borderwidth=2,
        itemsizing='trace'
    ),
    legend_title = dict(font = dict(family = "Arial", size = 60)),
)
Run Code Online (Sandbox Code Playgroud)

玩弄了物品大小,也一无所获。有人知道如何实现这一目标吗?

更新

根据下面的答案,我能够使线条变粗,但有一个限制。附上我认为的厚度极限(不知道确切尺寸)

在此输入图像描述

ves*_*and 7

答案:

根据您设置图形的方式,您可以使用:

fig.update_layout(legend=dict(itemsizing='constant'))
Run Code Online (Sandbox Code Playgroud)

或者:

fig.update_layout(legend=dict(itemsizing='trace'))
fig.update_traces(line=dict(width=12))
Run Code Online (Sandbox Code Playgroud)

细节:

在设置图形时,您似乎选择了非常细的线条。您可以使用以下方法将图例中的线条宽度与跟踪线的宽度“分离”:

fig.update_layout(legend=dict(itemsizing='constant'))
Run Code Online (Sandbox Code Playgroud)

由于您尚未生成可运行的代码片段,因此我将使用来自px.data.gapminder().

地块 1 -没有fig.update_layout(legend=dict(itemsizing='constant'))

在此输入图像描述

情节 2 -fig.update_layout(legend=dict(itemsizing='constant'))

在此输入图像描述

第三个选项是设置然后增加迹线fig.update_layout(legend=dict(itemsizing='trace'))图例的线宽,例如使用:fig.update_traces(line=dict(width=12))

情节 3 -fig.update_layout(legend=dict(itemsizing='trace'))

在此输入图像描述

包含所有可用选项的完整代码

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country')

fig.update_layout(showlegend=True)
fig.update_layout(legend = dict(bgcolor = 'yellow'))

#fig.update_layout(legend=dict(itemsizing='constant'))
fig.update_layout(legend=dict(itemsizing='trace'))
fig.update_traces(line=dict(width=12))

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