title: Linear regression to fit a power-law in Python
body: <p>I have two data sets <code>index_list</code> and <code>frequency_list</code> which I plot in a loglog plot by <code>plt.loglog(index_list, freq_list)</code>. Now I'm trying to fit a power law <code>a*x^(-b)</code> with linear regression. I expect the curve to follow the initial curve closely but the following code seems to output a similar curve but mirrored on the y-axis.
  I suspect I am using <code>curve_fit</code> badly.</p>
  <p>why is this curve mirrored on the x-axis and how I can get it to properly fit my inital curve?</p>
  <p><a href="https://norvig.com/google-books-common-words.txt" rel="nofollow noreferrer">Using this data</a></p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from scipy.optimize import curve_fit
  
  f = open (&quot;input.txt&quot;, &quot;r&quot;)
  index_list = []
  freq_list = []
  index = 0
  for line in f:
      split_line = line.split()
      freq_list.append(int(split_line[1]))
      index_list.append(index)
      index += 1
  
  plt.loglog(index_list, freq_list)
  def power_law(x, a, b):
      return a * np.power(x, -b)
  
  popt, pcov = curve_fit(power_law, index_list, freq_list)
  plt.plot(index_list,  power_law(freq_list, *popt))
  plt.show()
  </code></pre>