简体   繁体   中英

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/ )

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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