2013-04-17 61 views
4

我想從我的x軸標籤中刪除秒,因爲它們是不必要的。刪除matplotlib中x軸時間標籤的秒數

此外,我想中心對齊刻度標籤,而不是讓它們位於刻度標記的左側。

有關如何做到這一點的任何建議?

下面是一些我使用,如果這有助於

fig=plt.figure() 
ax=fig.add_subplot(111) 
line1 = plot(table.index,table[data],color=colors[0]) 

fig.autofmt_xdate(rotation=0) 

tickFormat = matplotlib.ticker.LinearLocator(numticks=5) 
ax.xaxis.set_major_locator(tickFormat)    

enter image description here

+0

你能告訴我們你是如何產生這個圖形的某些代碼的代碼?你只需要調整格式化程序的參數。 (但我不想猜你使用的格式化程序) – tacaswell

+0

我已經使用了fig.autofmt_xdate()進行格式化,是否有一個特定的格式化程序可以用來控制秒的外觀? –

回答

7
from matplotlib.dates import DateFormatter 
formatter = DateFormatter('%H:%M') 
plt.gcf().axes[0].xaxis.set_major_formatter(formatter) 
+1

還有一種方法可以調整自動格式化程序。請記住接受你自己的答案,當它允許你 – tacaswell

+0

關於字符串參數的格式,這裏是參考:http://strftime.org/ –

+1

tacaswell:*「還有一種方法來調整自動格式化器」 * - 怎麼樣?此外,如果您還使用具有浮點值的'ax.set_xticks()',則此解決方案會中斷:*「ValueError:DateFormatter找到x = 0的值,這是非法日期。通常發生這種情況通常是因爲您沒有通知軸表示它正在繪製日期,例如,使用ax.xaxis_date()「*。你怎麼能解決這個問題? – naught101