Python 如何在matplotlib中指定轴点的数量以及如何提取这些点?

Python 如何在matplotlib中指定轴点的数量以及如何提取这些点?,python,matplotlib,Python,Matplotlib,我有一个小脚本,它创建了一个matplotlib图,其中包含随机游走后的2000个随机点 我想知道是否有一种简单的方法来改变y轴上的点数,以及如何提取这些值 当我运行下面的代码时,我在Y轴上得到5个点,但我正在寻找一种方法将其扩展到20个点,并用这些值创建一个数组或序列。非常感谢 import matplotlib.pyplot as plt dims = 1 step_n = 2000 step_set = [-1, 0, 1] origin = np.zeros((1,dims)) rand

我有一个小脚本,它创建了一个matplotlib图,其中包含随机游走后的2000个随机点

我想知道是否有一种简单的方法来改变y轴上的点数,以及如何提取这些值

当我运行下面的代码时,我在Y轴上得到5个点,但我正在寻找一种方法将其扩展到20个点,并用这些值创建一个数组或序列。非常感谢

import matplotlib.pyplot as plt
dims = 1
step_n = 2000
step_set = [-1, 0, 1]
origin = np.zeros((1,dims))
random.seed(30)
step_shape = (step_n,dims)
steps = np.random.choice(a=step_set, size=step_shape)
path = np.concatenate([origin, steps]).cumsum(0)
plt.plot(path)
您可以使用定位器参数来指定刻度的数量。当然,您可以检索这些点。为此,必须使用ax创建一个子地块,然后使用get_yticks获取y_刻度

#second variant
# create subplot
fig, ax = plt.subplots(1,1, figsize=(20, 11))
img = ax.plot(path)
plt.locator_params(axis='y', nbins=20)
y_values = ax.get_yticks() # y_values is a numpy array with your y values

那太好了。非常感谢!!不客气!
#second variant
# create subplot
fig, ax = plt.subplots(1,1, figsize=(20, 11))
img = ax.plot(path)
plt.locator_params(axis='y', nbins=20)
y_values = ax.get_yticks() # y_values is a numpy array with your y values