在散点图中画一条给定方程式的线

2024-10-08 19:27:39 发布

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

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

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

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


Tags: 数据matplotlib绘制方程情节样子将线
2条回答

如果没有执行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

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

相关问题 更多 >

    热门问题