Tanvir Hossain Tanvir Hossain - 1 year ago 51
Python Question

Print data of a function in python

I have defined the following function. The output was fine as well. Is it possible to print the return value after using the function?

def runningMeanFast(x, N):
return np.convolve(x, np.ones((N,))/N)[(N-1):]

plt.figure(8)
plt.title('Sigupper vs time fitting')
#plot(time,SigUpper,'-r',label='Data')
plot(time[time_fit],runningMeanFast(SigUpper[time_fit],5),'b',label='convolution')
plt.grid()
xlabel(r'time')
ylabel(r'z/d')
#plt.ylim(1.5,6)
plt.grid()
plt.legend()
savefig('8.png')

Answer Source

Just add an intermediary step to print the result:

def runningMeanFast(x, N):
    result = np.convolve(x, np.ones((N,))/N)[(N-1):]
    print(result)
    return result

This will print the data, and then return it.

If you want to return the data then print it, then you should store the returned value, and print it after all your processing:

data = runningMeanFast(SigUpper[time_fit], 5)
plot(time[time_fit], data, 'b', label='convolution')
# Processing...
print(data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download