熊猫:如何在散点图中绘制一条线并将其带​​到后面/前面?

FaC*_*fee 2 python matplotlib pandas

我已经尽我最大的能力进行了检查,但没有找到任何kwds可以让您y=a-xpandas散点图(不一定是最适合的线)上画一条线(例如)并将其放在后面(或前面) )。

#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)
Run Code Online (Sandbox Code Playgroud)

有什么我可以用的吗?或者只是两个东西的绘制顺序?

jos*_*oto 5

您需要定义一个轴,然后将熊猫图传递给该轴。然后将任何线绘制到先前定义的轴上。这是一个解决方案。

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)
Run Code Online (Sandbox Code Playgroud)