我想创建一个包含主要和次要刻度以及主要和次要网格线的绘图。我希望网格线等距,但使用下面的代码,网格线不等距。我该如何修复它?
mnt = MonthLocator(range(1, 13), bymonthday=1, interval=2)
mnt_fmt = DateFormatter("%b '%y")
ax.xaxis.set_major_locator(mnt)
ax.xaxis.set_major_formatter(mnt_fmt)
ax.xaxis.set_minor_locator(MaxNLocator(20))
ax.yaxis.set_major_locator(MaxNLocator(5))
ax.yaxis.set_minor_locator(MaxNLocator(20))
# Create nice-looking grid for ease of visualization
ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=0.5)
最佳答案
您在 x 轴上看到的实际上是标记每个月的第一天和标记每个“x”天数之间的差异。
您已将主要刻度设置为每月的第一天。不过,您已将次要刻度设置为每 5 天定位一次。
由于每个月的第一天间隔不均匀,因此主要刻度不会均匀间隔。然而,小刻度总是相隔 5 天。因此,您会看到 x 轴上主要刻度线和次要刻度线的相对间距发生变化。
最后,您必须决定更想要哪个:每月一号滴答,或定期滴答。不幸的是,你不能两者兼得。
关于python - matplotlib 图中等距网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972132/