在散点图中给定方程式绘制一条线

问题描述 投票:0回答:2

我有一些数据点,我使用matplotlib绘制了散点图。现在,我想在同一散点图上为等式x + y = 0画一条线。这就是最终情节的样子。enter image description here

我现在拥有的是这个。enter image description here

如何将线x + y = 0添加到此散点图?

python matplotlib scatter-plot perceptron
2个回答
0
投票
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-20,20)

您可以简单地做:

plt.plot(x, -x)

或更笼统地说:

def f(x):
    return -x

plt.plot(x, f(x))

0
投票

如果未执行plt.show(),则可以绘制多个内容。

如果您没有将变量分配为图形,则您绘制的任何内容都会保存到不可见的空白表中,并在您决定时显示。

x = np.random.randint(1,100,10)
y = np.random.randint(1,100,10)

xx = np.arange(1,100)
yy = -xx

plt.scatter(x,y)
plt.plot(xx,yy)

plt.show()

您可以继续直到决定show()。结果是:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.