Search code examples
pythonplotly-python

plotly add_vline TypeError


Very simple to reproduce:

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)

gives this error message:

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`

However, when I remove the annotation_text variable it works:

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)

yields: working example

I don't understand why adding the annotation makes plotly want to do integer subtraction/addition with my x-timestamp.


Solution

  • I got the same error too. It seems that you don't support timestamps, as the error message in your question suggests. You can use fig.add_annotation() as a workaround.

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

    enter image description here