gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:57:31 30 4
gpt4 key购买 nike

我已尽我所能检查但未找到任何 kwds允许您在 y=a-x 上画一条线(例如 pandas )散点图(不一定是最佳拟合线)并将其带到后面(或前面)。

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

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

最佳答案

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

np.random.seed(365)  # for repeatable data
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)

enter image description here

关于python - Pandas :如何在散点图中绘制一条线并将其带​​到后面/前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732288/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com