title: How to visualize a nonlinear relationship in a scatter plot
body: <p>I want to visually explore the relationship between two variables. The functional form of the relationship is not visible in dense scatter plots like this:</p>
  
  <p><img src="https://i.stack.imgur.com/SzpBi.png" alt="scatter plot"></p>
  
  <p><strong>How can I add a lowess smooth to the scatter plot in Python?</strong></p>
  
  <p><strong>Or do you have any other suggestions to visually explore non-linear relationships?</strong></p>
  
  <p>I tried the following but it didn't work properly (drawing on an example from <a href="http://sourceforge.net/p/matplotlib/mailman/matplotlib-devel/thread/4BD33898.4030901@hawaii.edu/" rel="nofollow noreferrer">Michiel de Hoon</a>):</p>
  
  <pre><code>import numpy as np
  from statsmodels.nonparametric.smoothers_lowess import lowess
  x = np.arange(0,10,0.01)
  ytrue = np.exp(-x/5.0) + 2*np.sin(x/3.0)
  
  # add random errors with a normal distribution                      
  y = ytrue + np.random.normal(size=len(x))
  plt.scatter(x,y,color='cyan')
  
  # calculate a smooth curve through the scatter plot
  ys = lowess(x, y)
  _ = plt.plot(x,ys,'red',linewidth=1)
  
  # draw the true values for comparison
  plt.plot(x,ytrue,'green',linewidth=3)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/xgU8X.png" alt="lowess"></p>
  
  <p>The lowess smoother (red lines) is strange.</p>
  
  <p>EDIT:</p>
  
  <p>The following matrix also includes lowess smoothers (taken from <a href="https://stats.stackexchange.com/questions/64789/exploring-a-scatter-plot-matrix-for-many-variables">this question</a> on CV):
  <img src="https://i.stack.imgur.com/HLkIu.png" alt="enter image description here"></p>
  
  <p><strong>Does someone have the code for such a graph?</strong></p>