【问题标题】:Plotly Express set the width of a specific line on graph of multiple linesPlotly Express 在多条线图上设置特定线的宽度
【发布时间】:2022-07-07 01:35:16
【问题描述】:

我正在尝试使用 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()

产生这个:

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

【问题讨论】:

    标签: python line width stock plotly-express


    【解决方案1】:

    您可以在 fig.show() 之前添加此行以增加第 5 行的宽度 - fig['data'][4]['line']['width']=5,将 4 更改为 0-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()
    

    情节

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      相关资源
      最近更新 更多