2016-08-13 68 views
1

我有這個foll。據幀:matplotlib圖上主要和次要蜱的顯示不正確

Version  A2011  v1.0h 
Decade        
1510 - 1500 -3.553251 -0.346051 
1520 - 1510 -2.797978 -0.356409 
1530 - 1520 -2.194027 -0.358922 
1540 - 1530 -1.709211 -0.329759 
1550 - 1540 -1.354583 -0.308463 
1560 - 1550 -1.062436 -0.305522 
1570 - 1560 -0.821615 -0.293803 
1580 - 1570 -0.620067 -0.279270 
1590 - 1580 -0.465902 -0.271717 
1600 - 1590 -0.341307 -0.289985 
1610 - 1600 -0.365580 -0.491428 
1620 - 1610 -0.329492 -0.532413 
1630 - 1620 -0.299107 -0.568895 
1640 - 1630 -0.283209 -0.591281 
1650 - 1640 -0.267895 -0.595867 
1660 - 1650 -0.250805 -0.593352 
1670 - 1660 -0.240772 -0.539465 
1680 - 1670 -0.234985 -0.514080 
1690 - 1680 -0.230892 -0.497424 
1700 - 1690 -0.229585 -0.484620 
1710 - 1700 -0.853362 -0.892739 
1720 - 1710 -0.738257 -1.017681 
1730 - 1720 -0.660543 -0.966818 
1740 - 1730 -1.331018 -1.171711 
1750 - 1740 -1.271687 -1.541482 
1760 - 1750 -1.023931 -1.559551 
1770 - 1760 -1.089076 -1.757628 
1780 - 1770 -1.965483 -2.404880 
1790 - 1780 -1.579474 -2.167510 
1800 - 1790 -1.740528 -2.023357 
1810 - 1800 -2.237945 -2.804366 
1820 - 1810 -2.744933 -2.379714 
1830 - 1820 -3.706726 -3.717356 
1840 - 1830 -4.680707 -4.048362 
1850 - 1840 -5.836515 -4.660951 
1860 - 1850 -7.141815 -4.919932 
1870 - 1860 -5.847633 -2.972652 
1880 - 1870 -9.280493 -6.146244 
1890 - 1880 -8.815674 -6.689340 
1900 - 1890 -9.548756 -8.893766 
1910 - 1900 -10.596151 -10.115838 
1920 - 1910 -12.002151 -10.492217 
1930 - 1920 -12.524735 -11.155891 
1940 - 1930 -13.945205 -14.295251 
1950 - 1940 -13.877164 -13.609756 
1960 - 1950 -20.660728 -17.546248 
1970 - 1960 -14.495609 -15.537517 
1980 - 1970 -14.865093 -13.292412 
1990 - 1980 -16.254918 -13.626304 
2000 - 1990 -12.212572 -8.392916 

,我繪製它這樣:

 import matplotlib.pyplot as plt 
     from matplotlib.ticker import MaxNLocator 

     ax = df.plot() 

     # major ticks every 5, minor ticks every 1 
     ax.xaxis.set_major_locator(MaxNLocator(11)) 

     ax.grid(which='minor', alpha=0.2) 
     ax.grid(which='major', alpha=0.5) 

     ax.legend().set_visible(False) 
     plt.xticks(rotation=75) 
     plt.tight_layout() 
     plt.show() 

得出的數據是這樣的:

enter image description here

如何解決主要的和次要刻度,這樣的數量至少有10個主要價格和用戶指定的主要價格之間的次要價格?

+1

就像你使用的東西(我的意思是一個定位器)應該已經給你你需要的主要刻度的數量。請注意,您有很多主要的勾號,但由於某種原因,您似乎缺少勾號*標籤*。另外,你是否在任何地方導入該定位器?嗯......你不應該[旋轉xtick *標籤*](http://stackoverflow.com/questions/14852821/aligning-rotated-xticklabels-with-their-respective-xticks/14854007#14854007)? –

+0

謝謝@AndrasDeak,這也是讓我困惑的原因。不知道爲什麼代碼不按預期工作。我正在導入'從matplotlib.ticker導入MaxNLocator'。將更新代碼以反映 – user308827

回答

1

從環顧一下看來,熊貓似乎並不能很好地與定位器搭配。似乎設置tick標籤的首選方法是自動的。問題似乎是,數據與索引的隱式耦合(在自動設置刻度標籤時使用)與要設置的不同數量的刻度標籤混合在一起。

感覺應該有一個更好的方法(我沒有太多的熊貓經驗),但同時你可以使用一個主要的格式化程序來滾動自己的刻度標籤。無論如何,我的經驗是df.plot()是方便的,但如果你想確定,你應該直接使用matplotlib。

關鍵訣竅是用設置在x軸上的主要格式化半記載IndexFormatter,其中標籤來自數據幀的index

import matplotlib.pyplot as plt 
from matplotlib.ticker import MaxNLocator,IndexFormatter 

ax = df.plot() 

ax.xaxis.set_major_locator(MaxNLocator(11)) 
ax.xaxis.set_major_formatter(IndexFormatter(df.index)) # <-- new here 

ax.grid(which='minor', alpha=0.2) 
ax.grid(which='major', alpha=0.5) 

ax.legend().set_visible(False) 
plt.xticks(rotation=75) 
plt.tight_layout() 
plt.show() 

fixed major ticklabels

而原因是您的次要剔除是這樣的:

>>> ax.xaxis.get_minor_locator() 
<matplotlib.ticker.NullLocator at 0x7faf53d0e1d0> 

次要滴答的默認定位符是NullLocator,這實際上禁用次要的剔除,這反過來導致明顯缺乏次要的網格線。你應該選擇並設置一個合適的Locator作爲次要標記,並且一切都應該工作(換句話說,我不確定是否有一個簡單的方法來指定次要網格的數量)。

+1

偉大的解決方案,謝謝! – user308827