2016-08-13 69 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