'Plotting regression line with log y scale
I have two plots I want to show (the original data and then its regression line). Whenever I run this code, the regression line doesn't run through the data at all-- I think this has to do with plotting the original data on a log-scale for the y axis (I tried including this when running polyfit, but I'm still having issues).
a = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
b = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(a, b)
plt.yscale('log')
slope, intercept = np.polyfit(a, np.log(b), 1)
plt.plot(a, (slope*a)+intercept)
plt.show()
Solution 1:[1]
import numpy as np
import matplotlib.pyplot as plt
def regression(m,x,b):
return m * x + b
a = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
b = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
slope, intercept = np.polyfit(a, np.log(b), 1)
plt.figure()
plt.scatter(a, np.log(b))
plt.plot(a, regression(a,slope,intercept))
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JAdel |

