2017-03-17 84 views
2

我有以下情節(繪製使用matplotlib)。正如您在x軸中所看到的那樣,一年中的日期顯示爲0-364之間的數字。Python matplotlib分組的x軸值

Plot

但我想在12個等份分割x軸。輸出必須顯示月份的名稱,如Jan,Feb,Mar,...,Nov,Dec。有什麼已知的方法可以在matplotlib上實現。

+0

有一種已知的方式,例如參見[這裏](http://matplotlib.org/examples/pylab_examples/date_demo2.html)。順便說一下,您可以在軸標籤中引入LaTeX代碼以獲得更好的箭頭,例如'plt.xlabel(r'Days $ \ rightarrow $')'。同樣適用於圖例,標題等。字符串前面的「r」代表原始字符串。 – Michael

+0

這個鏈接你給了幫助。我能夠將X軸分成12個相等的部分。但我該如何將整數值從0到364轉換爲幾個月。 – Sourav

+0

我試過了,就像你可以看到[這裏](http://textuploader.com/dtxtw),儘管這可能不是最好的解決方案。對不起,我不知道如何在Stack Overflow的註釋中正確地發佈代碼。我希望你瞭解代碼的想法,所以你可以根據你的問題進行調整。 – Michael

回答

1

我認爲你只是想繪製一個月的定位器的日期。

%matplotlib inline 
from datetime import datetime 
import numpy 
from matplotlib import pyplot, dates, ticker 

# setup your date values (arbitrary year) 
oneday = datetime(1990, 1, 2) - datetime(1990, 1, 1) 
start = datetime(1990, 1, 1) 
end = datetime(1991, 1, 1) 
date = dates.drange(start, end, oneday) 

# make data 
values = numpy.random.uniform(low=-5, high=10, size=365).cumsum() 

# axis tooling 
locator = dates.MonthLocator() 
formatter = dates.DateFormatter('%b') 

# plot things, use the locators 
fig, ax = pyplot.subplots() 
ax.plot_date(date, values, '-') 
ax.xaxis.set_major_locator(locator) 
ax.xaxis.set_major_formatter(formatter) 

enter image description here

+0

感謝您的解決方案。有效 :) – Sourav