What format is datettime?

Hi,
I am using plotly and python.

I have a list of values that are supposed to be the x-axis.
It looks like this

[201909240908, 201909240956, 201909241002, 201909241056, 201909241113, 201909241156, ... ]

It is in YYYYMMDDhhmm format. How can I convert it to a datetime format that plotly can use intelligently?

Currently is looks like this. It just uses the datetime as a massive int

Convert your date integers to strings then to datetime objects with datetime.strptime. Something like this

datetime_ints = [201909240908, 201909240956, 201909241002]

datetimes = [datetime.strptime(str(dt_int), "%Y%m%d%H%M") for dt_int in datetime_ints]
1 Like

That worked, is there anyway to get accurate spacing between the x values?

Not sure why that’s happening. I set up a simple demo which has accurate spacing. Maybe you have some code that inadvertently changes the x-axis to be on a fixed interval.

import dash
import dash_core_components as dcc
import dash_html_components as html
import datetime

app = dash.Dash(__name__)

datetime_ints = [201909240908, 201909240956, 201909241002]

y = [10, 20, 50]
datetimes = [datetime.datetime.strptime(str(dt_int), "%Y%m%d%H%M") for dt_int in datetime_ints]

app.layout = html.Div([
    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': datetimes, 'y': y, 'mode': 'lines+markers'},
            ],
        }
    )
])

if __name__ == '__main__':
    app.run_server(debug=True)
1 Like

That worked, you have a different (better) way of creating the new datetimes list.

Thanks a bunch :smiley: @sjtrny