My Dataframe is in this format
Time AvgDiff
07:00:00 7.750782
07:01:00 9.306567
07:02:00 13.225209
07:03:00 18.346431
07:04:00 10.049761
07:05:00 18.567049
07:06:00 -4.903014
07:07:00 -10.66217
I did the following to create a synthetic date, the date is not important as my AvgDiff is the average of all dates for the particular time value
my_day = datetime.date(2018, 1, 15)
x_dt = [ datetime.datetime.combine(my_day, t) for t in df['Time'] ]
x = x_dt
y1 = df['AvgDiff']
plt.bar(x,y1, label='AvgDiff BPS', width=0.001) #bar to plot
plt.legend(loc='best')
plt.title('AvgDiff BPS')
plt.ylabel('BPS')
plt.xlabel('Time')
plt.gcf().autofmt_xdate()
plt.show()
This shows
It shows both the Date and Hours behind (in the X-Axis), However I need it to show just the Hours and Minutes.
I tried
FMT = '%H:%M:%S'
df['Time'] = df['Time'].map(lambda x: datetime.strptime(str(x), FMT))
df['Time'] = df['Time'].map(lambda x: x.replace(day=date, month=month, year=year))
plt.gcf().autofmt_xdate()
plt.show()
But it produces the exact same chart, why is that so?
To create a real-time plot, we need to use the animation module in matplotlib. We set up the figure and axes in the usual way, but we draw directly to the axes, ax , when we want to create a new frame in the animation.
Using the DateFormatter module from matplotlib, you can specify the format that you want to use for the date using the syntax: "%X %X" where each %X element represents a part of the date as follows: %Y - 4 digit year with upper case Y. %y - 2 digit year with lower case y. %m - month as a number with lower case m.
Ok I managed to solve this as per below
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
xformatter = mdates.DateFormatter('%H:%M')
plt.gcf().axes[0].xaxis.set_major_formatter(xformatter)
This produces Hour:Minute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With