简体   繁体   中英

Plotting time in x axis using matplotlib

I have the following dataset:

pressuredfjan1[['Unit','Time1']].head()
Out[53]: 
       Unit               Time1
0  321.3599 1970-01-01 00:00:40
1  321.3599 1970-01-01 00:04:40
2  321.6651 1970-01-01 00:06:40
3  320.7496 1970-01-01 00:10:40
4  322.2755 1970-01-01 00:14:40

I am trying to plot Unit against Time1 in an HH:MM:SS format. I've tried the following code:

dates = dates.date2num(list(pressuredfjan1['Time1']))
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(111)
ax1.plot(dates,pressuredfjan1['Unit'])
plt.show()

This gives me the plot, but I am not getting HH:MM:SS format in the x-axis. Instead, I'm getting 3.1, 3.2, 3.4...Le14 etc. Can somebody help me out here please? Thanks.

You can use:

import matplotlib as mpl

pressuredfjan1['dates'] = pd.to_datetime(pressuredfjan1['Time1']).dt.strftime('%H:%M:%S')
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(111)
majorFormatter = mpl.dates.DateFormatter('%H:%M:%S')
ax1.xaxis.set_major_formatter(majorFormatter)
plt.plot_date(pressuredfjan1['dates'], pressuredfjan1['Unit'])
plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM