![](/img/trans.png)
[英]How to add vertical line to legends, created via add_vline method in plotly python
[英]plotly add_vline TypeError
重現非常簡單:
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)
給出此錯誤消息:
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
變量時,它可以工作:
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 時間戳做 integer 減法/加法。
我也遇到了同樣的錯誤。 正如您問題中的錯誤消息所暗示的那樣,您似乎不支持時間戳。 您可以使用fig.add_annotation()
作為解決方法。
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.