gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:57:07 24 4
gpt4 key购买 nike

当我在图表中添加一条 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 withTimestamp 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

enter image description here

子图

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

enter image description here

关于python - 添加注释文本会产生整数错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69501854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com