首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >圆滑的add_vline TypeError

圆滑的add_vline TypeError
EN

Stack Overflow用户
提问于 2022-08-25 11:29:02
回答 1查看 60关注 0票数 0

很容易复制:

代码语言:javascript
复制
import pandas as pd
from plotly.offline import plot
df = pd.DataFrame(
    {'a': range(10), 'b':range(10, 20)},
    index=pd.date_range('2022-01-01', freq='H', periods=10)
)
fig = df.plot()
fig.add_vline(x=df.index[5], annotation_text='test')
plot(fig)

提供此错误消息:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\xander.blaauw\.conda\envs\pv_fx\lib\site-packages\IPython\core\interactiveshell.py", line 3397, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-af4ff0f10376>", line 9, in <cell line: 9>
    fig.add_vline(x=df.index[5], annotation_text='test')
  File "C:\Users\xander.blaauw\.conda\envs\pv_fx\lib\site-packages\plotly\basedatatypes.py", line 4085, in add_vline
    self._process_multiple_axis_spanning_shapes(
  File "C:\Users\xander.blaauw\.conda\envs\pv_fx\lib\site-packages\plotly\basedatatypes.py", line 4031, in _process_multiple_axis_spanning_shapes
    augmented_annotation = shapeannotation.axis_spanning_shape_annotation(
  File "C:\Users\xander.blaauw\.conda\envs\pv_fx\lib\site-packages\plotly\shapeannotation.py", line 216, in axis_spanning_shape_annotation
    shape_dict = annotation_params_for_line(
  File "C:\Users\xander.blaauw\.conda\envs\pv_fx\lib\site-packages\plotly\shapeannotation.py", line 63, in annotation_params_for_line
    eX = _mean(X)
  File "C:\Users\xander.blaauw\.conda\envs\pv_fx\lib\site-packages\plotly\shapeannotation.py", line 7, in _mean
    return float(sum(x)) / len(x)
  File "pandas\_libs\tslibs\timestamps.pyx", line 311, in pandas._libs.tslibs.timestamps._Timestamp.__add__
  File "pandas\_libs\tslibs\timestamps.pyx", line 296, in pandas._libs.tslibs.timestamps._Timestamp.__add__
TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported.  Instead of adding/subtracting `n`, use `n * obj.freq`

但是,当我移除annotation_text变量时,它可以工作:

代码语言:javascript
复制
import pandas as pd
from plotly.offline import plot
df = pd.DataFrame(
    {'a': range(10), 'b':range(10, 20)},
    index=pd.date_range('2022-01-01', freq='H', periods=10)
)
fig = df.plot()
fig.add_vline(x=df.index[5])
plot(fig)

产量:

我不明白为什么添加注释使plotly希望使用x时间戳执行整数减法/加法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-25 14:43:56

我也犯了同样的错误。似乎您不支持时间戳,正如问题中的错误信息所暗示的那样。您可以使用fig.add_annotation()作为解决方案。

代码语言:javascript
复制
import pandas as pd
import datetime
#from plotly.offline import plot
pd.options.plotting.backend = "plotly"
df = pd.DataFrame(
    {'a': range(10), 'b':range(10, 20)},
    index=pd.date_range('2022-01-01', freq='H', periods=10)
)
fig = df.plot()
fig.add_vline(df.index[5])
fig.add_annotation(x=df.index[5], y=18, text='test', showarrow=False)

fig.show()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73486684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档