我正在尝试绘制一个显示网格的圆。我写了以下脚本,给出了下图。但是,轴上的标签相互干扰。如何使标签显示(..,-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=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()