'Linear regression plot on log scale in Python
I want to do linear regression to the data given by x and y. Everything seems to be fine when I use a linear plot, but when I want to plot it on a log scale the line does not look straight. I think I should divide the interval into finer grids rather than only six points. But I couldn't do that.
How can I do line fitting on a log scale for the below script?
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1560., 526., 408., 226., 448., 288.])
y = np.array([0.118, 0.124, 0.131, 0.160, 0.129, 0.138])
f = np.multiply(x,y**2)
coefs = np.polyfit(x, f, 1)
pred_f = coefs[1] + np.multiply(sorted(x), coefs[0])
fig, ax1 = plt.subplots(1, 1, figsize=(8,6))
ax1.scatter(x, f)
ax1.plot(sorted(x), pred_f, 'k--')
ax1.set_xscale('log')
ax1.set_yscale('log')
plt.show()
Thank you in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
