使用matplotlib绘制主要和次要网格线及刻度

1 投票
1 回答
5735 浏览
提问于 2025-04-18 17:38

我有两个很大的整数

min=round(raw_min,-5) # a negative number
max=round(raw_max,-5)

从中我得到了一个有趣的刻度范围:

xticks=np.arange(min,max,500000)

在x轴上,我想要有一些小刻度(包括标签),这些刻度是针对xticks范围的。此外,我还想在值0的位置有一个大刻度和网格线。我尝试添加:

minorLocator = FixedLocator(xticks)
majorLocator = FixedLocator([0])

ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))
ax.xaxis.set_minor_locator(minorLocator)
plt.tick_params(which='both', width=1)
plt.tick_params(which='major', length=7, color='b')
plt.tick_params(which='minor', length=4, color='r')
ax.yaxis.grid(True)
ax.xaxis.grid(b=True,which='major', color='b', linestyle='-')

但是没有成功……

在这里输入图片描述

没有小刻度,也没有大刻度的网格线。

有什么想法吗?

1 个回答

2

看起来我漏掉了以下这一行:

plt.grid(b=True,which='both')

撰写回答