matplotlib图中的等间距网格线

时间:2016-01-24 04:11:20

标签: python matplotlib

我想创建一个包含主要和次要刻度以及主要和次要网格线的图。我希望网格线是等间距的,但是使用下面的代码,网格线不是等间距的。我该如何解决?

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)

EDIT enter image description here

1 个答案:

答案 0 :(得分:1)

您在x轴上看到的实际上是每个月的第一个标记和标记每个" x"天数。

您已将主要刻度设置为每个月的第一天。但是,您已将次要刻度设置为每5天定位一次。

因为每个月的第一个不是均匀间隔,所以主要的蜱不会均匀分布。但是,次要蜱总是相隔5天。因此,您会看到x轴上主刻度和次刻度的相对间距的变化。

最后,你必须决定你想要的更多:每个月的第一天蜱,或定期蜱。不幸的是,你不能同时拥有这两者。

相关问题