Python matplotlib绘图上主刻度和次刻度显示不正确

Python matplotlib绘图上主刻度和次刻度显示不正确,python,pandas,matplotlib,Python,Pandas,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.30846

我有福勒。数据帧:

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,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()
我是这样画的:

        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()
结果图如下所示:


如何确定主刻度和次刻度的数量,使主刻度之间至少有10个主刻度,以及用户指定的次刻度数?

环顾四周,熊猫似乎无法很好地使用定位器。设置标签的首选方法似乎是自动的。问题似乎在于,自动设置勾号标签时使用的数据与索引的隐式耦合与要设置的不同数量的勾号标签混淆

感觉应该有一个更好的方法(我对熊猫没有太多经验),但同时你可以使用一个主要的格式化程序来滚动你自己的标签。无论如何,我的经验是,
df.plot()
很方便,但如果您想确定,应该直接使用matplotlib

关键技巧是使用半文档格式设置x轴的主格式化程序,其标签来自数据帧的
索引

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,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()
导入matplotlib.pyplot作为plt
从matplotlib.ticker导入MaxNLocator、IndexFormatter
ax=df.plot()
ax.xaxis.set_major_定位器(MaxNLocator(11))
ax.xaxis.set_major_formatter(IndexFormatter(df.index))#>>ax.xaxis.get_minor_locator()

次要记号的默认定位器是
NullLocator
,它实际上禁用次要记号,从而导致明显缺少次要网格线。您应该为次要刻度选择并设置一个合适的
定位器
,然后一切都应该正常(换句话说,我不确定是否有一种简单的方法可以根据主要刻度指定次要网格的数量)。

类似于您正在使用的东西(我指的是定位器)应该已经为您提供了所需的主要刻度的数量。请注意,您有很多主要刻度,但由于某些原因,您似乎缺少刻度标签。另外,您是否在任何地方导入该定位器?嗯……你不应该吗?谢谢@AndrasDeak,这也是让我困惑的地方。不确定代码为什么不能按预期工作。我正在从matplotlib导入
。ticker import MaxNLocator
。将更新代码以反映这一点