tags:

views:

233

answers:

1

Here is my code:

import matplotlib.pyplot as plt
plt.loglog(length,time,'--')

where length and time are lists.

How do I find the slope of this graph?

+6  A: 

If you have matplotlib then I believe you must also have numpy installed. If that's true, then you could use numpy.polyfit to find the slope:

import matplotlib.pyplot as plt
import numpy as np

length=np.random.random(10)
length.sort()
time=np.random.random(10)
time.sort()
slope,intercept=np.polyfit(np.log(length),np.log(time),1)
print(slope)
plt.loglog(length,time,'--')
plt.show()
unutbu
This would be true if the graph is a line. In case it is a curve and the slope changes at different points a diffrential is needed. Try the diff function.
whatnick
Can you please provide some more details about the function.
Bruce
@Peter: `polyfit` (in its simplest incarnation) takes 3 args: the `x`-data, `y`-data, and the degree of polynomial. Since you are looking for a linear fit, the 3rd arg is set to 1. `polyfit` then returns the coefficients of the best-fit polynomial, which in this case means the slope and y-intercept. HTH.
unutbu