How to transform nonlinear model to linear?
<p>I'm ananlyzing a dataset, and I know that the data should follow a power model:</p>

<pre><code>y = a*x**b
</code></pre>

<p>I transformed it to linear by taking logarithms:</p>

<pre><code>ln(y) = ln(a) + b* ln(x)
</code></pre>

<p>However, the problems arised on adding a trend line to the plot</p>

<pre><code>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')
</code></pre>

<p><a href="http://i.stack.imgur.com/DkDKP.png" rel="nofollow"><img src="http://i.stack.imgur.com/DkDKP.png" alt="enter image description here"></a></p>

<p>This is what I get with this code.
How to modify the code, so that the trend line on the plot would be correct?</p>

<p>Your green strange plot is what you get when you do a line plot in <code>matplotlib</code>, with the <code>x</code> values unsorted. It's a line plot, but it connects by lines <em>(x, y)</em> pairs jumping right and left (in your specific case, it looks like back to near the x-origin). That gives these strange patterns. </p>

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

<p>Try calling the plot after sorting both arrays according to the indices of the first using <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html"><code>numpy.argsort</code></a>, say </p>

<pre><code>wetarea_x[np.argsort(wetarea_x)]
</code></pre>

<p>and</p>

<pre><code>yy[np.argsort(wetarea_x)]
</code></pre>

python matplotlib plot scipy linear-regression