添加注释文本会产生整数错误消息

Adding annotation text yields integer error message

当我在我的图表中添加一条 vline 时,它​​完美地工作:

fig.add_vline(
        x=pd.to_datetime('1970-01-01 00:00:00'),
        line_dash='dot',
        row=0)

[![在此处输入图片描述][1]][1]

当我尝试在 vline 旁边添加注释文本时,我收到一条错误消息

fig.add_vline(
    x=pd.to_datetime('1970-01-01 00:00:00'),
    line_dash='dot',
    annotation_text='15',
    row=0)

TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported. Instead of adding/subtracting n, use n * obj.freq

  • 作为解决方法,对 add_vline() 进行一次调用,对 add_annotation()
  • 进行一次调用
  • 已将参数设置为 add_annotation(),以便注释位于行首
import pandas as pd
import plotly.express as px

ROWS = 20
df = pd.DataFrame({"ColA":pd.date_range("1-jan-1970", freq="4H", periods=ROWS), "ColB":np.random.uniform(1,30, ROWS)})

fig = px.line(df, x="ColA", y="ColB")
fig.update_xaxes(title_font=dict(size=12), tickformat='%X')

for time in df["ColA"].values:
    time = pd.Timestamp(time).to_pydatetime()
    fig.add_vline(x=time)
    fig.add_annotation(x=time, y=1, yref="paper", text="15")

fig

子图

import pandas as pd
import plotly.express as px

ROWS = 20
df = pd.DataFrame(
    {
        "ColA": pd.date_range("1-jan-1970", freq="4H", periods=ROWS),
        "ColB": np.random.uniform(1, 30, ROWS),
        "Facet": np.repeat(["plot 1", "plot 2"], ROWS // 2),
    }
)

fig = px.line(df, x="ColA", y="ColB", facet_row="Facet")
fig.update_xaxes(title_font=dict(size=12), tickformat="%X")

for time, y, facet in df[["ColA", "ColB", "Facet"]].values:
    time = pd.Timestamp(time).to_pydatetime()
    fig.add_vline(x=time, row=0 if facet == "plot 1" else 1)
    fig.add_annotation(x=time, y=y, text="15", row=0 if facet == "plot 1" else 1, col=0)

fig