简体   繁体   English

python-fig.add_shape 和 add_annotation 的问题

[英]python- problem with fig.add_shape and add_annotation

I have written this code and it is not giving me the solution that want.我写了这段代码,但它没有给我想要的解决方案。 Can anyone help me and tell me what mistake I am making ?谁能帮助我并告诉我我犯了什么错误?

CODE:代码:在此处输入图片说明

    fig=px.line(df_italy,x='Date',y='infection_rate_italy')
    fig.add_shape(
        dict(
        `enter code here`type="line",
         x0=italy_lockdown_start_date,
         y0=0,
         x1=italy_lockdown_start_date,
         y1=df_italy['infection_rate_italy'].max(),
         line=dict(color='red',width=2)
    )
)
fig.add_annotation(
    dict(
        x= italy_lockdown_start_date,
        y=df_italy['infection_rate_italy'].max(),
        text='starting date of lockdown'
    )
)
fig.show()

my output:我的输出:

在此处输入图片说明

the output that I am interested in is:我感兴趣的输出是:在此处输入图片说明

I would add hovertext when plotting the original line (following https://plotly.com/python/hover-text-and-formatting/ )我会在绘制原始行时添加hovertext(遵循https://plotly.com/python/hover-text-and-formatting/

fig = px.Figure()
fig.add_trace(go.Scatter(
    x=df['date'], # pls, modify accordingly
    y=df['n_cases'], # pls modify accordingly
    hovertemplate= "<b>%{text}</b><br><br>" +
        "Date: %{x|%Y/%m/%d}<br>" +
        "Infection rate: %{y:.0%}<br>" +
        "Population: %{marker.size:,}" +
        "<extra></extra>"
        ))

Vertical lines part:竖线部分:

fig=px.line(df_italy,x='Date',y='infection_rate_italy')
fig.add_trace(go.Scatter(
    x=[italy_lockdown_start_date, italy_lockdown_start_date],
    y=[0, df_italy['infection_rate_italy'].max()],
    mode="lines",
    line=go.scatter.Line(color="red"),
    showlegend=False))
fig.add_annotation(
    dict(
        x= italy_lockdown_start_date,
        y=df_italy['infection_rate_italy'].max(),
        text='starting date of lockdown'
    )
)
fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM