gpt4 book ai didi

python - 如何在 plotly 线图中有选择地隐藏图例?

转载 作者:行者123 更新时间:2023-12-04 14:48:51 26 4
gpt4 key购买 nike

我正在努力为我的线条图中的一些但不是所有线条隐藏图例。这是 plotly 现在的样子。
Plot:
enter image description here
本质上,我想隐藏浅灰色线条的图例,同时将其保留在彩色线条的位置。
这是我的代码:

import plotly.graph_objects as go

fig = go.Figure()
fig.update_layout(autosize=False, width=800, height=500, template='none')
fig.update_layout(title = 'Title', xaxis_title = 'Games', yaxis_title = 'Profit')

for team in rest_teams:
fig.add_traces(go.Scatter(x=df['x'], y = df[team], name = team, line = {'color': '#F5F5F5'}))

for team in big_eight:
line_dict = {'color': cmap[team]}
fig.add_traces(go.Scatter(x=df['x'], y = df[team], name = team, line = line_dict))

fig.show()
我可以更新布局
fig.update_layout(showlegend=False)
这隐藏了整个事情并且不是最佳的。帮助将不胜感激。

最佳答案

如果我正确理解您想要的输出,您可以使用 showlegend = False对于您使用 color = #F5F5F5 设置灰色的轨迹:

for c in cols1:
fig.add_trace(go.Scatter(x = df.index, y = df[c], line_color = '#F5F5F5',
showlegend = False))
然后将其保留在您希望为其分配颜色的行中,并确保包含 name = c在:
for c in cols2:
fig.add_trace(go.Scatter(x = df.index, y = df[c],
name = c))
阴谋:
enter image description here
完整代码:
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd


df = px.data.stocks()
df = df.set_index('date')

fig = go.Figure()

cols1 = df.columns[:2]
cols2 = df.columns[2:]

for c in cols1:
fig.add_trace(go.Scatter(x = df.index, y = df[c], line_color = '#F5F5F5',
showlegend = False))

for c in cols2:
fig.add_trace(go.Scatter(x = df.index, y = df[c],
name = c))
fig.update_layout(template=template
fig.show()

关于python - 如何在 plotly 线图中有选择地隐藏图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69442203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com