2013-07-17 254 views
6

我想绘制一些时间戳(年 - 月 - 日 - 小时 - 分钟 - 秒格式)。我使用下面的代码,但它不显示任何小时分秒信息,它将它们显示为00-00-00。我仔细检查了我的日期数组,并且从下面的代码片段可以看到,它们不是零。使用Matplotlib绘制时间戳(小时/分钟/秒)

你知道我为什么得到00-00-00吗? enter image description here

import matplotlib.pyplot as plt 
import matplotlib.dates as md 
import dateutil 

dates = [dateutil.parser.parse(s) for s in datestrings] 
# datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000'] 
plt.subplots_adjust(bottom=0.2) 
plt.xticks(rotation= 80) 
ax=plt.gca() 
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S') 
ax.xaxis.set_major_formatter(xfmt) 
plt.plot(dates[0:10],plt_data[0:10], "o-") 
plt.show() 

回答

7

尝试在你的图形放大,你会看到日期时间为你的x轴尺度的变化扩大。

plotting unix timestamps in matplotlib

我想要绘制染色体阳性选择的热图时,有一个同样恼人的问题。如果我放大太远,事情就会完全消失!

编辑:此代码完全按照您给出的日期绘制日期,但不会在两者之间添加刻度。

import matplotlib.pyplot as plt 
import matplotlib.dates as md 
import dateutil 

datestrings = ['2012-02-21 11:28:17.980000', '2012-02-21 12:15:32.453000', '2012-02-21 23:26:23.734000', '2012-02-26 17:42:15.804000'] 
dates = [dateutil.parser.parse(s) for s in datestrings] 

plt_data = range(5,9) 
plt.subplots_adjust(bottom=0.2) 
plt.xticks(rotation=25) 

ax=plt.gca() 
ax.set_xticks(dates) 

xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S') 
ax.xaxis.set_major_formatter(xfmt) 
plt.plot(dates,plt_data, "o-") 
plt.show() 
+0

感谢您的回答。我已经看过“在matplotlib中绘制unix时间戳”问题。但是,我的问题是,Matplotlib不显示小时分秒,因为它显示在“绘制matplotlib中的unix时间戳”问题上。在我的情况下,它只是将它们显示为00-00-00,我找不出原因(因为它好像我的时间戳是正确的)。 – user1048858

+0

你说得对,这是因为它试图用蜱填充x轴,看到我编辑的答案。 – seth

0

我可以告诉你它为什么显示00:00:00。这是因为那是特定日子的开始时间。例如,2012-02-22 00:00:00(2012-02-22午夜12点)和2012-02-23 00:00:00(2012年2月23日12点12点) )。

这两个时间之间的时间戳蜱没有显示。

我自己想弄清楚如何在这些时间之间显示刻度。

相关问题