Sergey Gulbin - 8 months ago 46

Python Question

I'm ananlyzing a dataset, and I know that the data should follow a power model:

`y = a*x**b`

I transformed it to linear by taking logarithms:

`ln(y) = ln(a) + b* ln(x)`

However, the problems arised on adding a trend line to the plot

`slope, intercept, r_value, p_value, std_err = scipy.stats.mstats.linregress(x_ln, y_ln)`

yy = np.exp(intercept)*wetarea_x**slope

plt.scatter(wetarea_x, arcgis_wtrshd_x, color = 'blue')

plt.plot(wetarea_x, yy, color = 'green')

This is what I get with this code.

How to modify the code, so that the trend line on the plot would be correct?

Answer

Your green strange plot is what you get when you do a line plot in `matplotlib`

, with the `x`

values unsorted. It's a line plot, but it connects by lines *(x, y)* pairs jumping right and left (in your specific case, it looks like back to near the x-origin). That gives these strange patterns.

You don't have this problem with the blue plot, because it's a scatter plot.

Try calling the plot after sorting both arrays according to the indices of the first using `numpy.argsort`

, say

```
wetarea_x[np.argsort(wetarea_x)]
```

and

```
yy[np.argsort(wetarea_x)]
```