title: Best fit line semilog scale with linear y-axis and log x-axis
body: <p>My question is closely related to the following topic on SO:
  <a href="https://stackoverflow.com/questions/48361637/fit-straight-line-on-semi-log-scale-with-matplotlib">Fit straight line on semi-log scale with Matplotlib</a></p>
  
  <p>However, I want to create a best fit line in a chart where the X-axis is logarithmic and the Y-axis is linear.</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  
  plt.scatter(players['AB'], players['Average'], c='black', alpha=0.5)
  
  p = np.polyfit(players['AB'], players['Average'], 1)
  plt.plot(players['AB'], p[0] + p[1] * np.log(players['AB']), color='r', linestyle='dashed', alpha=0.7)
  
  plt.xscale('log')
  plt.xlim(1, 25000)
  plt.ylim(-0.05, 0.60)
  plt.xlabel('Number of at-bats (AB)')
  plt.ylabel('Batting Average')
  plt.show()
  </code></pre>
  
  <p>This gives me the following:</p>
  
  <p><a href="https://i.stack.imgur.com/XP2UX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XP2UX.png" alt="enter image description here"></a></p>
  
  <p>What am I doing wrong? Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>  p = np.polyfit(np.log(players['AB']), players['Average'], 1)
    plt.plot(players['AB'], p[0] + p[1] * np.log(players['AB']), color='r', linestyle='dashed', alpha=0.7)
  </code></pre>
  
  <p>This still gives me the wrong best fit:
  <a href="https://i.stack.imgur.com/TtWze.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TtWze.png" alt="enter image description here"></a></p>