1
votes

How does plotly works with datetime ? Can't I plot datetime x datetime objects in plotly? This question is due to some problems when plotting "hours / minutes" in the y axis, it goes messy.

The data

In the example my hours plot starts at 11:13 and ends at 10:54, but the range is 08:33 to 11:55

When plotting in matplotlib, it recognizes the range ( what I want) but in dash it creates a ladder from the lowest values to the highest values

Yes, I've already tried to 'yaxis':{tickformat:'%HH:%MM:%SS'

1

1 Answers

1
votes

You can convert columns to appropriate type and then plot it using plotly. Here are useful page to doing this: ref. Code:

# import necessaries libraries
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
from datetime import datetime

# Create data
df = pd.DataFrame({"date": ['01/08/2018', '02/08/2018', '06/08/2018',
                            '10/08/2018', '12/08/2018', '13/08/2018',
                            '15/08/2018'],
                   "time": ['06:40:00', '05:57:00', '02:56:00', '03:57:00',
                            '12:24:00', '10:48:00', '15:56:00']})
# For now, columns data are not recognized
print(df.dtypes)
# Convert columns to datetime and plotly should recognize it
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')
df['time'] = pd.to_datetime(df['time'], format='%H:%M:%S')
# You see, that type of columns changed
print(df.dtypes)
# Build a plot
data = [go.Scatter(x=df.time, y=df.date)]
# Plot a plot
py.plot(data, filename='time-series-simple.html', auto_open=False)

Output: Graph