我必须在代码中手动设置x
的范围(这里是50到500):
data = plb.loadtxt('data.txt')
x = data[:,0]
y= data[:,1]
cs = UnivariateSpline(x, y)
xs = np.arange(50, 500, .1)
plt.plot(x, y, label='A')
plt.plot(xs, cs(xs), label="B")
plt.xlim(50, 500)
如何编写np.arange
和plt.xlim
来获取由一组给定的x
的最低和最高值数据(从文件中读取)?
最佳答案
import numpy as np
x_min = x.min() # or np.min(x)
x_max = x.max() # or np.max(x)
xs = np.arange(x_min, x_max+0.1, .1)
plt.xlim(x_min, x_max)
需要+0.1
,因为停止值被排除在np.arange
中。
注意:如果您想要值的完整扩展,通常不需要手动设置 xlim
。我相信默认的 xlim
已经使用了 x 值的 min
和 max
(可能有点修改)。
关于python - 如何根据 SciPy 中的给定数据设置 numpy.arange 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616318/