24beads
24beads

Reputation: 201

Xaxis date labels not formating plotly pyhton

I am trying to shorten size of the xaxis labels on my graph. I would like to only have hour and minute. At the moment my .csv file holds it in the full format '23/09/2022 00:10:00', etc Tried to applied some formatting found in google but it does not seem to update my graph. Anyone could advise on this please?

df = two_days_data.sort_values(by='Date')
mask = df["TTT"] == station

fig = px.line(df[mask], x='Date', y="AAA", title='BBB.', markers=True)
fig.update_xaxes(type='category', tickformat="%H %M")

return fig

enter image description here

enter image description here

Upvotes: 0

Views: 286

Answers (2)

24beads
24beads

Reputation: 201

df['Date'] = pd.to_datetime(df['Date']).dt.strftime('%d.%m - %H:%M')


fig.update_xaxes(type='category', tickangle=45)

This has given me what I wanted.

Thank you!

Upvotes: 0

Redox
Redox

Reputation: 9967

There are two things you will need to do... first - make sure the df.Date column is in datetime format. You can do this by using pd.to_datatime()

df['Date'] = pd.to_datetime(df['Date']) ## Add format if you get an error to specify non-standard format, if present

Then use the tickformat as below.

fig.update_xaxes(tickformat="%H:%M")

This should give you the plot with the x-axis labels in HH:MM format

My full data

    Date    name    AAA
0   2022-09-21 15:10:00 Some name 1 224
1   2022-09-21 15:10:00 Some name 2 239
2   2022-09-21 15:10:00 Some name 3 254
3   2022-09-21 15:10:00 Some name 4 269
4   2022-09-21 15:10:00 Some name 5 284
5   2022-09-21 15:10:00 Some name 6 299
6   2022-09-21 15:10:00 Some name 7 314
7   2022-09-21 15:10:00 Some name 8 329
8   2022-09-21 16:10:00 Some name 1 230
9   2022-09-21 16:10:00 Some name 2 239
10  2022-09-21 16:10:00 Some name 3 248
11  2022-09-21 16:10:00 Some name 4 257
12  2022-09-21 16:10:00 Some name 5 266
13  2022-09-21 16:10:00 Some name 6 275
14  2022-09-21 16:10:00 Some name 7 284
15  2022-09-21 16:10:00 Some name 8 293
16  2022-09-21 17:10:00 Some name 1 200
17  2022-09-21 17:10:00 Some name 2 213
18  2022-09-21 17:10:00 Some name 3 226
19  2022-09-21 17:10:00 Some name 4 239
20  2022-09-21 17:10:00 Some name 5 252
21  2022-09-21 17:10:00 Some name 6 265
22  2022-09-21 17:10:00 Some name 7 278
23  2022-09-21 17:10:00 Some name 8 291
24  2022-09-21 18:10:00 Some name 1 304
25  2022-09-21 18:10:00 Some name 2 317
26  2022-09-21 18:10:00 Some name 3 330
27  2022-09-21 18:10:00 Some name 4 343
28  2022-09-21 18:10:00 Some name 5 356
29  2022-09-21 18:10:00 Some name 6 369
30  2022-09-21 18:10:00 Some name 7 382
31  2022-09-21 18:10:00 Some name 8 395
32  2022-09-21 19:10:00 Some name 1 408
33  2022-09-21 19:10:00 Some name 2 421
34  2022-09-21 19:10:00 Some name 3 434
35  2022-09-21 19:10:00 Some name 4 447
36  2022-09-21 19:10:00 Some name 5 460
37  2022-09-21 19:10:00 Some name 6 473
38  2022-09-21 19:10:00 Some name 7 486
39  2022-09-21 19:10:00 Some name 8 499

Code

df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y %H:%M')
mask = df["name"] == "Some name 1"

fig = px.line(df[mask], x='Date', y="AAA", title='BBB.', markers=True)
fig.update_xaxes(tickformat="%H %M")

Plot

enter image description here

Upvotes: 1

Related Questions