Python 使用matplotlib无法正确绘制时间从12变为1的时间
当时间从12变为1时,我无法找到如何使用matplotlib正确绘制时间。我从一个文本文件中提取时间,并试图将其与该文件中的另一列进行对比。文本文件如下所示:Python 使用matplotlib无法正确绘制时间从12变为1的时间,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,当时间从12变为1时,我无法找到如何使用matplotlib正确绘制时间。我从一个文本文件中提取时间,并试图将其与该文件中的另一列进行对比。文本文件如下所示: 1, 12:58:39, 1.903 0.002 0.000 0.000 2, 12:58:41, 3.888 0.003 0.000 0.000 3, 12:58:43, 6.287 0.005 0.000 0.000
1, 12:58:39, 1.903 0.002 0.000 0.000
2, 12:58:41, 3.888 0.003 0.000 0.000
3, 12:58:43, 6.287 0.005 0.000 0.000
...
39, 12:59:55, 55.512 0.066 0.000 0.000
40, 12:59:57, 54.850 0.068 0.000 0.000
41, 12:59:59, 53.940 0.069 0.000 0.000
42, 01:00:01, 52.865 0.071 0.000 0.000
43, 01:00:03, 52.368 0.073 0.000 0.000
44, 01:00:05, 51.954 0.075 0.000 0.000
45, 01:00:06, 57.497 0.059 0.000 0.00
etc..
这是我的代码:
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
from io import StringIO
from matplotlib.dates import bytespdate2num
import numpy as np
with open('C:/Users/example/{}.txt'.format(TestNumber)) as fh:
io = StringIO(fh.read().replace(',', '\t'))
e1, time, e2, e3, e4, e5 = np.loadtxt(io, unpack=True,converters={1:bytespdate2num('%H:%M:%S')})
fig, ax = plt.subplots()
# Plot the date using plot_date
ax.plot_date(time, e2,'-')
# Choose your xtick format string
date_fmt = '%H:%M:%S'
# Use a DateFormatter to set the data to the correct format.
date_formatter = mdate.DateFormatter(date_fmt)
ax.xaxis.set_major_formatter(date_formatter)
# Sets the tick labels diagonal so they fit easier.
fig.autofmt_xdate()
ax.set_xlabel('Time')
fig.savefig('pfig1.png')`
当运行时间不从12变为1时,它会正确地绘制,看起来是这样的
当使用从12到1的时间运行时,它会绘制如下的内容,这是不正确的。
时间开关从12到1的绘图:
有解决方法吗?如果从AM切换到PM似乎有问题,为什么不在示例输入中包含一些PM行。从文件输入中,如何知道是AM还是PM?似乎没有标记表明这一点。情节似乎是正确的。当从12:59到01:00时,这是一个12小时的时间跳跃,这在图中正确表示。