2016-02-14 14 views
5

Tôi đang âm mưu một con số mà các định dạng mặc định xuất hiện như:Matplotlib ngày thao tác sao cho đánh dấu năm xuất hiện mỗi 12 tháng

Year tick shows up every 12 months, but months show only every 3 months

Tôi muốn sửa đổi nó để tháng ticks xuất hiện mỗi 1 tháng nhưng giữ lại năm. nỗ lực hiện tại của tôi là thế này:

years = mdates.YearLocator() 
months = mdates.MonthLocator() 
monthsFmt = mdates.DateFormatter('%b-%y') 
dts = s.index.to_pydatetime() 

fig = plt.figure(); ax = fig.add_subplot(111) 
ax.plot(dts, s) 
ax.xaxis.set_major_locator(months) 
ax.xaxis.set_major_formatter(monthsFmt) 

nhưng nó không tạo ra kết quả đúng:

Not right

Làm thế nào chính xác để tôi cần phải sửa đổi nó để nó xuất hiện như là người đầu tiên nhưng với những tháng bọ ve xuất hiện mỗi tháng?

Trả lời

7

Đã tìm ra một giải pháp, đó là để gắn các tháng vào các ve nhỏ và giữ năm là chính.

Ví dụ:

years = mdates.YearLocator() 
months = mdates.MonthLocator() 
monthsFmt = mdates.DateFormatter('%b') 
yearsFmt = mdates.DateFormatter('\n\n%Y') # add some space for the year label 
dts = s.index.to_pydatetime() 

fig = plt.figure(); ax = fig.add_subplot(111) 
ax.plot(dts, s) 
ax.xaxis.set_minor_locator(months) 
ax.xaxis.set_minor_formatter(monthsFmt) 
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90) 
ax.xaxis.set_major_locator(years) 
ax.xaxis.set_major_formatter(yearsFmt) 

Kết quả trong: enter image description here

Các vấn đề liên quan