2017-08-10 27 views
2

我不得不手動設置的x(這裏從50到500)範圍中的代碼:如何設置SciPy中給定數據的numpy.arange的值?

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.arangeplt.xlim採取由一組給定的x的最低和最高值的數據(從文件中讀取)?

回答

3

你可以只使用np.minnp.max:需要

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值的minmax(可能有點修改)。

+0

這兩個提示都非常方便!我很愚蠢的想念'xlim'參數是可選的。 – Googlebot

相關問題