我正在尝试使用 Python 分析来自各种不同文件的数据。所以我使用一个如下所示的函数:
def multi_estimate(a , b):
ids = np.linspace(0.3 , 0.7 ,num = 17, endpoint = True) #files name identifier
for i in ids:
dat = np.loadtxt('Qn'+ str(int(i*1000))+'.txt') # reading filename
q = np.array(dat[:,1]); # take the second column
x , y = hurst(q); # calculate lags and R/S values for Hurst exponent
coef = linear_fit(x, y , a , b) # make a linear fit and estimate slope
return None
在我的线性拟合函数中,我正在绘制结果,因为我想检查拟合是否正确以及各点是否在一条直线上。如果没有正确完成,我想通过在我的函数中添加一些代码来进行新的拟合。我的问题是,在执行过程中,会出现空数字,并且仅当 for 循环结束时才会填充图形。
如何让每个图出现在第 I 步的每个图上,检查它,然后继续下一个图?
我的拟合函数是:
def linear_fit(x, y, xmin, xmax):
idx1 = (np.abs(x-xmin)).argmin()
idx2 = (np.abs(x-xmax)).argmin()
coef = np.polyfit(np.log2(x[idx1:idx2+1]), np.log2(y[idx1:idx2+1]), 1)
plt.figure()
plt.plot(np.log2(x), np.log2(y), 'r+', label = 'data')
plt.plot(np.log2(x[idx1:idx2+1]) , coef[1]+coef[0]*np.log2(x[idx1:idx2+1]), label = 'H = %1.2f' %coef[0] )
plt.grid()
plt.legend(loc = 'best')
plt.show()
return coef
最佳答案
嗯,这对我有用。
import matplotlib.pyplot as pl
def test(x):
pl.figure()
pl.plot(x)
pl.show()
for i in range(1,3):
eje = range(i*10)
test(eje)
每次函数调用我都会得到一张图。
关于python - 在每一步的 for 循环中绘制 matplotlib 图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33925857/