0

For a class I am needing to make a lot of plots in a logarithmic space and get a line of best fit to find the slope. I seem to be getting correct values for the slope and intercept when I do it the way I show below, but the graph I produce shows the line of best fit as almost step-like instead of an actual smooth line. Here is the function I have been using to make the plots:

def scatterlinearfitlogscale(X, Y, xlab, ylab, title, color, colorline):
plt.scatter(X, Y, color=color)
plt.xscale('log')
plt.yscale('log')
plt.xlabel(xlab, fontsize='16')
plt.ylabel(ylab, fontsize='16')
plt.title(title, fontsize='20', fontweight='bold')
m, b = np.polyfit(X, Y, 1)
plt.plot(X, m*np.array(X)+b, linewidth=2, color=colorline)
plt.show() 

This produces (when applied to a set of log'd data, in this case, rowing records, not super relevant): Graphs of rowing records X number of oarspeople

Does anyone know why the line is shown step-like that way? My biggest concern: am I making an error when having the line calculated, or is this due to how it is being displayed in a logarithmic space?

Thank you!

update: thank you so much! plotting it on a linear scale with the log'd values gave me the straight line I was looking for. I should have realized this sooner!

  • The line will look straight on a linear scale. Because you scale your axis with log transform, we do not expect the line to be straight. If you believe there is a linear relationship between the logarithm of number of oarspeople and the logarithm of best times, then you should fit the linear model after applying log transform to each variable. – LarryBird Sep 08 '22 at 02:48

0 Answers0