python - 在每一步的 for 循环中绘制 matplotlib 图形

标签 python matplotlib plot

我正在尝试使用 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/

相关文章:

python - 有没有办法在discord.py中接收其他用户给我的建议

python - 仅水平网格(在 python 中使用 pandas plot + pyplot)

matlab - 绘制一条粗细不同的线

python - 以 AM/PM 格式绘制 Pandas Datetime 时间序列

image - 在 MATLAB 中绘制图像背景

python - 当它被传递给另一个字典的更新方法时拦截字典 __getitem__ 方法调用

python - 在 Python 中定义函数时,如何将标题作为参数调用?

python - 日期和图表对齐 - 经济分析

python - 在 WxPython 面板中嵌入 Seaborn 图

python - Django / python : understanding how super is used in function