title: How to fit single polynomial curve to scatter plot
body: <pre><code>[from sklearn.mixture import GaussianMixture
  from sklearn import preprocessing
  import numpy as np
  import matplotlib.pyplot as plt
  import pandas as pd
  from scipy import stats
  from astropy.io import ascii
  from scipy.stats import norm
  import math
  from sklearn.metrics import r2_score
  
  data2  = pd.read_csv(&quot;Dispersion total.csv&quot;)
  names = data2.columns
  df = pd.DataFrame(data2, columns=names)
  df.head()
  
  
  data3 = pd.read_csv(&quot;Dispersion less than equal to -1.5.csv&quot;)
  names1 = data3.columns
  df1 = pd.DataFrame(data3, columns=names1)
  df1.head()
  
  x= df1['Mean Mag'].values
  y=df1['Log(sigma)'].values
  
  #print(y)
  
  w= df['Mean Mag'].values
  z=df['Log(sigma)'].values
  
  
  #ax=data2.plot(kind= 'scatter',x= 'Mean Mag',y = 'Log(sigma)',color='green')
  #data3.plot(kind= 'scatter',x= 'Mean Mag',y = 'Log(sigma)',ax=ax)
  
  fit = np.polyfit(x, y, 2)
  a = fit[0]
  b = fit[1]
  c = fit[2]
  fit_equation = a * np.square(x) + b * x + c
  #Plotting
  fig1 = plt.figure()
  ax1 = fig1.subplots()
  ax1.plot(x, fit_equation,color = 'r',alpha = 0.5, label = 'Polynomial fit')
  ax1.scatter(w,z, s = 4, color = 'b', label = 'Data points')
  ax1.set_title('Polynomial fit example')
  ax1.legend()
  
  plt.xlabel('Mean Magnitude')
  plt.ylabel('Log(sigma)')
  plt.show()
  </code></pre>
  <p>This is the plot I currently have : <a href="https://i.stack.imgur.com/pws4M.png" rel="nofollow noreferrer">Current</a></p>
  <p>I want a polynomial fit to my data that doesn't try to fit every point but a general curve in the most dense region (between -1.75 to -1.5).Something like this:</p>
  <p><a href="https://i.stack.imgur.com/Q5m2W.png" rel="nofollow noreferrer">What I want it to look like</a></p>