簡體   English   中英

如何指定 matplotlib 中的軸點數以及如何提取這些點?

[英]How do I specify the number of axis points in matplotlib and how do I extract theese points?

我有一個小腳本,它創建一個 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)
import matplotlib.pyplot as plt
import numpy as np
import random

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)
#first variant 
plt.plot(path)
plt.locator_params(axis='x', nbins=20)
plt.locator_params(axis='y', nbins=20)

您可以使用 locator_params 來指定刻度數。 當然,您可以檢索這些點。 為此,您必須使用 ax 創建一個子圖,然后您可以使用 get_yticks 獲取 y_ticks。

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM