2012-06-19 103 views
7

我正在使用matplotlib以hh:mm:ss.ms格式繪製數據作爲時間的函數,其中ms爲毫秒。但是,我沒有看到情節中的毫秒。是否可以添加它們?在matplotlib中顯示毫秒

dates = matplotlib.dates.datestr2num(x_values) # convert string dates to numbers 
plt.plot_date(dates, y_values) # doesn't show milliseconds 
+1

嘿那裏,請張貼一些代碼,顯示你在做什麼。這樣,它更容易幫助,歡呼。 – fraxel

+0

您可能需要添加一些限定符來定義顯示的小數位數,例如%.2f。但是,如果沒有看到更多的代碼,就很難知道。 – cosmosis

+0

可以,請舉個例子嗎?謝謝 – Bob

回答

8

這裏的問題是,有一類格式蜱,並plot_date設置一個類的東西,你不想要的:一個自動格式化,從來沒有繪製毫秒。

爲了改變這種情況,您需要從matplotlib.dates.AutoDateFormatter更改爲您自己的格式化程序。 matplotlib.dates.DateFormatter(fmt)使用datetime.strftime格式字符串創建格式化程序。我不知道如何獲得這個顯示毫秒,但它會顯示微秒,我希望它會爲你工作;畢竟它只是一個額外的零。試試這個代碼:

dates = matplotlib.dates.datestr2num(x_values) # convert string dates to numbers 
plt.plot_date(dates, y_values) # doesn't show milliseconds by default. 

# This changes the formatter. 
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:%M:%S.%f")) 

# Redraw the plot. 
plt.draw()