可能重复:
Creating graph with date and time in axis labels with matplotlib
我不知道如何在使用matplotilib绘图时更改日期格式,而我的数据在我的字典中有完整的日期,我只绘制小时,分钟,秒
from datetime import datetime
import matplotlib.pyplot as plt
dico = {'A01': [(u'11/10/12-08:00:01', 2.0), (u'11/10/12-08:10:00', 10.0), \
(u'11/10/12-08:20:01', 5.0), (u'11/10/12-08:30:01', 15.0), \
(u'11/10/12-08:40:00', 7.0), (u'11/10/12-08:50:01', 45.0)],
'A02': [(u'11/10/12-08:00:01', 10.0), (u'11/10/12-08:10:00', 12.0), \
(u'11/10/12-08:20:01', 15.0), (u'11/10/12-08:30:01', 10.0), \
(u'11/10/12-08:40:00', 17.0), (u'11/10/12-08:50:01', 14.0)]}
x = []
y = []
for key in sorted(dico.iterkeys()):
#in Python3
#for key in sorted(dico.keys()):
points = [(datetime.strptime(i[0], "%d/%m/%y-%H:%M:%S"), \
i[1]) for i in dico[key]]
points.sort()
x, y = zip(*points)
plt.plot(x, y, label=key)
# plotting
plt.gcf().autofmt_xdate()
plt.legend(loc='upper right')
plt.xlabel('Dates')
plt.ylabel("titre")
plt.title("Modbus")
plt.show()
答案 0 :(得分:15)
解决方案:
from matplotlib.dates import DateFormatter
formatter = DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)