title: Fitting a straight line to a log-log curve in matplotlib
body: <p>I have a plot with me which is logarithmic on both the axes. I have pyplot's <code>loglog</code> function to do this. It also gives me the logarithmic scale on both the axes.</p>
  
  <p>Now, using numpy I fit a straight line to the set of points that I have. However, when I plot this line on the plot, I cannot get a straight line. I get a curved line. 
  <img src="https://i.stack.imgur.com/aMP9n.png" alt="The blue line is the supposedly &quot;straight line&quot;. It is not getting plotted straight. I want to fit a straight line to the curve plotted by red dots"></p>
  
  <p>The blue line is the supposedly "straight line". It is not getting plotted straight. I want to fit this straight line to the curve plotted by red dots</p>
  
  <p>Here is the code I am using to plot the points:</p>
  
  <pre><code>import numpy
  from matplotlib import pyplot as plt
  import math
  fp=open("word-rank.txt","r")
  a=[]
  b=[]
  
  for line in fp:
      string=line.strip().split()
      a.append(float(string[0]))
      b.append(float(string[1]))
  
  coefficients=numpy.polyfit(b,a,1)
  polynomial=numpy.poly1d(coefficients)
  ys=polynomial(b)
  print polynomial
  plt.loglog(b,a,'ro')
  plt.plot(b,ys)
  plt.xlabel("Log (Rank of frequency)")
  plt.ylabel("Log (Frequency)")
  plt.title("Frequency vs frequency rank for words")
  plt.show()
  </code></pre>