1

I am trying to analyse data using Python from various different files. So I use a function that looks like this:

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

In my linear fit function I am plotting the result because I would like to check if the fit is done properly and the points are in a straight line. If it is not done properly I would like to make a new fit by adding some code in my function. My problem is that during execution empty figures appear and the are filled with the plots only when the for loop comes to end.

How can I get each plot to appear at each for step I, check it and then move on to the next one?

My fit function is:

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

2 Answers 2

1

Well, this works for me.

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)

I get one plot for each function call.

Sign up to request clarification or add additional context in comments.

7 Comments

I am also getting one figure at each step. The problem is that the plots are drawn in each figure at the end of the for loop and not at each step so there is nothing to examine. By the way i am using Canopy
Could you try that code above? Does it show one plot (with data) in each iteration or do all the plots appear at the end of the for loop? I'm using Python 2.7.10.
if you see my linear fit function i already use your suggestion except if i am missing something.
No, I mean for you to copy/paste the code above and check if you have the same 'problem' than with your code: all plotting done after the for loop ends. Are you running your code inside 'Canopy' 's IDE? If so, see if it works using python's IDE. Maybe it's a Canopy bug/characteristic.
i have used your example and the result is the same. Plots are shown at the end of the execution. I am using Canopy as IDE and working enviroment.
|
0

It turns out that when i am using Ipython from a terminal everything work as they are expected to work. Problems appear when i am using Canopy, so i guess this is a Canopy Bug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.