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?
You can find the line's slope using the linregress() function if we define the x and y coordinates as arrays. The following code uses the linregress() method of the SciPy module to calculate the slope of a given line in Python.
Matplotlib: Graph/Plot a Straight Line The slope equation y=mx+c y = m x + c as we know it today is attributed to René Descartes (AD 1596-1650), Father of Analytic Geometry. The equation y=mx+c y = m x + c represents a straight line graphically, where m is its slope/gradient and c its intercept.
If you have matplotlib then you must also have numpy installed since it is a dependency. Therefore, 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With