简体   繁体   English

使用 matplotlib 在 x 轴上绘制时间

[英]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.我正在尝试以 HH:MM:SS 格式针对Time1绘制Unit 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.这给了我情节,但我没有在 x 轴上获得 HH:MM:SS 格式。 Instead, I'm getting 3.1, 3.2, 3.4...Le14 etc. Can somebody help me out here please?相反,我得到了 3.1、3.2、3.4...Le14 等。有人可以帮我吗? 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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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