如何在python中更改matplotlib中标记x轴和y轴的频率?
我正在尝试绘制一个带有网格的圆。我写了下面的脚本,它给出了下面的图片。但是,轴上的标签相互干扰。如何使标签显示(…,-10,-5,0,5,10…),保持网格如下图所示?。我想保持网格单元的尺寸为1*1 我试图使用plt.locator_params(),但网格单元的尺寸发生了变化并变得更大如何在python中更改matplotlib中标记x轴和y轴的频率?,python,matplotlib,grid,axis,xticks,Python,Matplotlib,Grid,Axis,Xticks,我正在尝试绘制一个带有网格的圆。我写了下面的脚本,它给出了下面的图片。但是,轴上的标签相互干扰。如何使标签显示(…,-10,-5,0,5,10…),保持网格如下图所示?。我想保持网格单元的尺寸为1*1 我试图使用plt.locator_params(),但网格单元的尺寸发生了变化并变得更大 import numpy as np import matplotlib.pyplot as plt import math from matplotlib.pyplot import figure R1
import numpy as np
import matplotlib.pyplot as plt
import math
from matplotlib.pyplot import figure
R1=28
n=64
t=np.linspace(0, 2*np.pi, n)
x1=R1*np.cos(t)
y1=R1*np.sin(t)
plt.axis("square")
plt.grid(True, which='both', axis='both')
plt.xticks(np.arange(min(x1)-2,max(x1)+2, step=1))
plt.yticks(np.arange(min(y1)-2,max(y1)+2, step=1))
#plt.locator_params(axis='x', nbins=5)
#plt.locator_params(axis='y', nbins=5)
plt.plot(x1,y1)
plt.legend()
plt.show()
不是matplotlib专家,因此可能有更好的方法,但可能如下所示:
from matplotlib.ticker import MultipleLocator
...
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot(x1,y1)
ax.xaxis.set_minor_locator(MultipleLocator())
ax.xaxis.set_major_locator(MultipleLocator(5))
ax.yaxis.set_minor_locator(MultipleLocator())
ax.yaxis.set_major_locator(MultipleLocator(5))
ax.grid(True, which='both', axis='both')
plt.show()
不是matplotlib专家,因此可能有更好的方法,但可能如下所示:
from matplotlib.ticker import MultipleLocator
...
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot(x1,y1)
ax.xaxis.set_minor_locator(MultipleLocator())
ax.xaxis.set_major_locator(MultipleLocator(5))
ax.yaxis.set_minor_locator(MultipleLocator())
ax.yaxis.set_major_locator(MultipleLocator(5))
ax.grid(True, which='both', axis='both')
plt.show()
非常感谢您的回答@我非常感谢你的回答@大本