I have a time series, which is a
pandas.Series indexed by a
pandas.DatetimeIndex. That this series is hourly (one point per hour). When I plot it, and try to overlay on the graph some shaded areas with
axvspan(), with time points that are with higher precision, those data are truncated to hourly precision.
Here is a minimal example:
#!/usr/bin/env python3 import matplotlib.pyplot as plt import pandas as pd dates = pd.DatetimeIndex(['2021-01-01 08:00:00', '2021-01-01 09:00:00', '2021-01-01 10:00:00']) ts_temp = pd.Series([0.5, 0.2, 0.7], index=dates) fig, ax1 = plt.subplots() ts_temp.plot(ax=ax1) ax1.axvspan('2021-01-01 08:30:00', '2021-01-01 09:30:00', facecolor='orange', alpha=0.15) plt.show()
I have hourly points, which are plotted well. But you can see that the shaded areas, which is supposed to start at 8:30 and stop at 9:30, is instead “truncated” to 8:00 to 9:00. See the plot:
Why is this happening, and how can it be fixed?