如何在python中更改matplotlib中标记x和y轴的频率?
How to change the frequency of labeling the x and y axis in matplotlib in python?
我正在尝试绘制一个显示网格的圆。我写了下面的脚本,给出了下面的图片。但是,轴上的标签相互干扰。如何使标签出现 (..,-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()
我正在尝试绘制一个显示网格的圆。我写了下面的脚本,给出了下面的图片。但是,轴上的标签相互干扰。如何使标签出现 (..,-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()