r/learnpython • u/xolopx • 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()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()
View all comments
Show parent comment
2
u/xolopx May 13 '20
My word, thank you for pointing this out to me. I am not sure I would have ever cottoned on to the fact that it was the width that was screwing me. It makes a lot more sense now as to why the scale of the plot's xaxis was changing with the width value. I was able to very quickly and easily implement a solution with your advice. By taking the smallest difference in timestamps I determined the x-axis scale ( inspired by this stackoverflow post ).
You know when it feels like there must be a simple solution to your problem, this was definitely one of those times.