Pandas: how to plot a line in a scatter and bring it to the back/front?
<p>I have checked to the best of my capabilities but haven't found any <code>kwds</code> that allow you to draw a line (such as <code>y=a-x</code>) on a <code>pandas</code> scatter plot (not necessarily the line of best fit) and bring it to the back (or to the front).</p>

<pre><code>#the data frame
ax=df.plot(kind='scatter', x='myX', y='myY',title="Nice title", 
   xlim=[0,100],ylim=[0,100],figsize=(8,5), grid=True,fontsize=10)

#the line
lnsp=range(0,110,10)
line=[100-i for i in lnsp] #line is y=100-x
ax=line_df.plot(kind='line',color='r',ax=ax,legend=False,grid=True,linewidth=3)
</code></pre>

<p>Is there anything I can use? Or is it just the order in which the two things are drawn?</p>

<p>You need to define an axis, and then pass the pandas plot to that axis. You then plot whatever line to that previously defined axis. Here is a solution.</p>

<pre><code>x = np.random.randn(100)
y = np.random.randn(100)
line = 0.5*np.linspace(-4, 4, 100)
x_line = np.linspace(-4, 4, 100)

fig, ax = plt.subplots(figsize=(8,5))
df = pd.DataFrame({"x": x, "y":y})
#You pass the wanted axis to the ax argument
df.plot(kind='scatter', x='x', y='y',title="Nice title", grid=True,fontsize=10, ax=ax) 
ax.plot(line, x_line, zorder=-1)
</code></pre>

python pandas matplotlib