Pandas:如何在一个散点中画一条线,并将它放到后面/前面?

2024-05-18 18:22:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尽我所能进行了检查,但没有发现任何kwds允许您在pandas散点图上绘制一条线(例如y=a-x),并将其放在后面(或前面)。在

#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)

有什么我可以用的吗?还是仅仅是这两样东西画的顺序?在


Tags: thetruepandasdfdataplottitleline
1条回答
网友
1楼 · 发布于 2024-05-18 18:22:00

您需要定义一个轴,然后将pandas绘图传递到该轴。然后绘制与先前定义的轴的任何直线。这里有一个解决方案。在

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)

相关问题 更多 >

    热门问题