Plotly Express在多行的图上设置特定行的宽度

发布于 2025-02-13 09:56:30 字数 1016 浏览 1 评论 0原文

我正在尝试使用plotly.express使“关闭”线更宽。我尝试了有条件的语句或添加跟踪的各种选项,但似乎没有任何作用。

fig.for_each_trace(
lambda trace: trace.update(line=dict(color="Black", width=3)) if trace.name == "Close" else ()

这是代码:

import plotly.express as px
ticker = 'TSLA'
colours = ['steelblue', 'tan', 'black', 'red', 'green']
fig = px.line(df.tail(150), x=df.tail(150).index, y=['middle_band', 'SMA_200', 
'upper_band', 'lower_band','Close'], title=f'{ticker}', 
color_discrete_sequence=colours, width=800, height=600)
fig.update_traces(textfont_size=12, showlegend=True)
fig.update_layout(legend=dict(
yanchor="top",
y=0.9,
xanchor="left",
x=0.02
))
fig.update_xaxes(title_text='Price evolution over time',  title_font = {"size": 16},)
fig.update_yaxes(title_text='Price', tickprefix="$")
fig.show()

产生以下方式:

一如既往,欢迎任何帮助。

I'm trying to make the 'Close' line wider than the others using plotly.express. I tried various options with a conditional statement, or adding a trace but nothing seems to work.

fig.for_each_trace(
lambda trace: trace.update(line=dict(color="Black", width=3)) if trace.name == "Close" else ()

This is the code:

import plotly.express as px
ticker = 'TSLA'
colours = ['steelblue', 'tan', 'black', 'red', 'green']
fig = px.line(df.tail(150), x=df.tail(150).index, y=['middle_band', 'SMA_200', 
'upper_band', 'lower_band','Close'], title=f'{ticker}', 
color_discrete_sequence=colours, width=800, height=600)
fig.update_traces(textfont_size=12, showlegend=True)
fig.update_layout(legend=dict(
yanchor="top",
y=0.9,
xanchor="left",
x=0.02
))
fig.update_xaxes(title_text='Price evolution over time',  title_font = {"size": 16},)
fig.update_yaxes(title_text='Price', tickprefix="
quot;)
fig.show()

which produces this:
TESLA price evolution over time

As always, any help is welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2025-02-20 09:56:30

您可以在图show()之前添加此行,以增加第五行的宽度 - fig ['data'] [4] ['line'] ['width'] = 5,在0-4之间更改4个数字将使宽度 /厚度增加5(或您需要的任何数字)。我使用了随机数并绘制了您的代码。下面也是如此。

import plotly.express as px
df = pd.DataFrame(np.random.randint(0,1000,size=(5, 5)), columns=list('ABCDE'))
ticker = 'TSLA'
colours = ['steelblue', 'tan', 'black', 'red', 'green']
fig = px.line(df.tail(5), x=df.tail(5).index, y=['A', 'B', 
'C', 'D','E'], title=f'{ticker}', 
color_discrete_sequence=colours, width=800, height=600)
fig.update_traces(textfont_size=12, showlegend=True)
fig.update_layout(legend=dict(
yanchor="top",
y=0.9,
xanchor="left",
x=0.02
))
fig.update_xaxes(title_text='Price evolution over time',  title_font = {"size": 16},)
fig.update_yaxes(title_text='Price', tickprefix="$")
#fig.update_traces(line=dict(color="Black", width=0.5))
fig['data'][4]['line']['width']=5
fig.show()

plot

”在此处输入图像说明”

You can add this line just before fig.show() to increase the width of the 5th line - fig['data'][4]['line']['width']=5, changing the 4 to any number between 0-4 will increase the width / thickness by 5 (or any number you need). I have used random numbers and plotted your code. Same is below.

import plotly.express as px
df = pd.DataFrame(np.random.randint(0,1000,size=(5, 5)), columns=list('ABCDE'))
ticker = 'TSLA'
colours = ['steelblue', 'tan', 'black', 'red', 'green']
fig = px.line(df.tail(5), x=df.tail(5).index, y=['A', 'B', 
'C', 'D','E'], title=f'{ticker}', 
color_discrete_sequence=colours, width=800, height=600)
fig.update_traces(textfont_size=12, showlegend=True)
fig.update_layout(legend=dict(
yanchor="top",
y=0.9,
xanchor="left",
x=0.02
))
fig.update_xaxes(title_text='Price evolution over time',  title_font = {"size": 16},)
fig.update_yaxes(title_text='Price', tickprefix="
quot;)
#fig.update_traces(line=dict(color="Black", width=0.5))
fig['data'][4]['line']['width']=5
fig.show()

Plot

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文