Skip to content
Advertisement

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?

JavaScript

Thank you in advance.

Advertisement

Answer

Indeed, “straight lines” (linear functions) don’t look straight on log-log plots:

JavaScript

Result:

enter image description here


To fit on the log scale, run your regression on loagrithms of the original data:

JavaScript

Plot:

enter image description here

Or have Matplotlib draw log ticks. Then you’ll need to exponentiate pred_f to put it on the same scale as the data:

JavaScript

The plot is the same, but it now uses the original scale of your data:

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement