r/learnpython • • May 12 '20

Matplotlib time series plotting headache.

I am trying to plot (bar graph) some values pairs consisting of integers, y and timestamps x. The timestamps are datetime.datetime objects.Things work as expected IF data pairs do not have a timestamp that shares the same date as another. This is an issue as new data is generated every 3 seconds and so many pairs share the same date. The result of plotting is a single data point occuring for each date only.

I wish to be able to plot data points against an x-axis whose resolution is in seconds not days. Please see below the code that I'm attempting to implement.

This implementation works, note the dates do not lie on the same day.

import matplotlib.pyplot as plt
import datetime

x = [datetime.datetime(2010, 12, 1, 10, 10, 10),
     datetime.datetime(2011, 1, 4, 9, 0),
     datetime.datetime(2011, 5, 5, 9, 0)]
y = [4, 9, 2]

fig, ax = plt.subplots(1, 1)
ax.bar(x, y, width=10)
plt.show()

Successful Plot

This implementation does not work, note the dates are all on the same day.

import matplotlib.pyplot as plt
import datetime

dates = ["2020-05-11 18:25:37","2020-05-11 18:25:40","2020-05-11 18:25:43","2020-05-11 18:25:46","2020-05-11 18:25:49"]
X = [datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') for date in dates]
Y = [1, 3, 4, 6, 4]

fig, ax = plt.subplots(1, 1)
ax.bar(x, y, width=10)
plt.show()

Abomination

11 Upvotes

15 comments sorted by

View all comments

Show parent comment

1

u/xolopx May 13 '20

Thank you so much for your response, that insight about matplotlib not liking datetimes is very valuable. I was able to implement your simpler solution with success. I come from a java background and am finding the documentation for python a little discouraging, but your guide set me on the right path.

2

u/DanteRadian May 13 '20

Hey, I am glad i was able to help!

Have a good day!

Also i suggest using this Python Tutor for Visualizing Python Code! if codes are giving you a hard day. Use Python 3.6 with Anaconda to visualize numpy and pandas. Its still experimental so not sure if support for matplotlib has been implemented yet! But yeah its something that has helped me a lot and hopefully helps you too in the long run!

1

u/xolopx May 13 '20

Wow looks very helpful, a picture is always easier to understand, cheers.