Timeseries plot with timedelta axis

Hello,

Is there anyway to show timedelta data in β€˜%H:%M:%S’ format on the plot? Below is my simple code. I want the x axis ticks to be in %M:%S format.

Thanks

import pandas as pd
import plotly.graph_objs as go

from plotly.offline import init_notebook_mode
from plotly.offline import iplot 
init_notebook_mode(connected=True)

df = pd.DataFrame({'x': range(1000), 'y':np.sin(np.arange(1000)/100)})
df.x = pd.to_timedelta(df.x, unit='s')
trace = go.Scatter(x=df.x, y=df.y)
data = [trace]
fig = go.Figure(data)
iplot(fig)

1 Like

Hi @Shahab_ai,

No, this isn’t directly supported right now. See https://github.com/plotly/plotly.py/issues/799 and https://github.com/plotly/plotly.py/issues/801. Perhaps the workaround in https://github.com/plotly/plotly.py/issues/801#issuecomment-317174985 would be helpful.

-Jon

1 Like

Bummer! Basic python data type support would be great

Hi @Shahab_ai ,

Instead using to_timedelta try to use to_datetime to convert x column.
Also you need to format the xaxis ticks using '%M-%S format and update the layout.

Hope this help.

import numpy as np
import pandas as pd
import plotly.graph_objs as go


df = pd.DataFrame({'x': range(1000), 'y':np.sin(np.arange(1000)/100)})
df.x = pd.to_datetime(df.x, unit='s')
trace = go.Scatter(x=df.x, y=df.y)
data = [trace]
fig = go.Figure(data)

fig.update_layout(xaxis_tickformat='%M-%S')
fig.show()