0

I am struggling with properly visualizing timestamp data in matplotlib. My data is as follows with timestamp being an index column:

timestamp
2021-04-10 14:00:10    0.036350
2021-04-10 14:00:20    0.036350
2021-04-10 14:00:30    0.036350
2021-04-10 14:00:40    0.036350
2021-04-10 14:00:50    0.036221
                         ...
2021-04-10 15:14:40    0.036043
2021-04-10 15:14:50    0.036042
2021-04-10 15:15:00    0.036126
2021-04-10 15:15:10    0.036109
2021-04-10 15:15:20    0.036109

I am trying to plot as follows:

fig, ax1 = plt.subplots()
ax1.set(xlabel='Time', ylabel='Value')
ax1.plot(df, color='g')
plt.show()

Giving the following results: enter image description here

As you can see, the ticks labels are not visible and very much overlapping. I would expect some default integrated matplotlib functionality to have ticks 'aggregated' by minute or hour based on the timestamp data?

I tried the following:

ax1.xaxis.set(
    minor_locator=mdates.HourLocator(),
    minor_formatter=mdates.DateFormatter("%H-%M"),
)

But that only generates even more ticks.. Probably the solution is quite easy, but i have not found it yet.

Bart
  • 264
  • 1
  • 2
  • 11
  • 1
    Convert your time strings to datetime. – Jody Klymak Apr 10 '21 at 19:22
  • Does [matplotlib: how to prevent x-axis labels from overlapping each other](https://stackoverflow.com/questions/13515471/matplotlib-how-to-prevent-x-axis-labels-from-overlapping-each-other) answer your question? – wwii Apr 10 '21 at 23:44
  • @JodyKlymak wauw, i dont know why I didn't find this while searching for the answer. Thanks. – Bart Apr 12 '21 at 19:20

0 Answers0