如何在python中的散点图上过度绘制一条线? [英] How to overplot a line on a scatter plot in python?

查看:36
本文介绍了如何在python中的散点图上过度绘制一条线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据向量,并将它们放入matplotlib.scatter().现在我想过度绘制这些数据的线性拟合.我该怎么做?我试过使用 scikitlearnnp.scatter.

I have two vectors of data and I've put them into matplotlib.scatter(). Now I'd like to over plot a linear fit to these data. How would I do this? I've tried using scikitlearn and np.scatter.

推荐答案

import numpy as np
from numpy.polynomial.polynomial import polyfit
import matplotlib.pyplot as plt

# Sample data
x = np.arange(10)
y = 5 * x + 10

# Fit with polyfit
b, m = polyfit(x, y, 1)

plt.plot(x, y, '.')
plt.plot(x, b + m * x, '-')
plt.show()

这篇关于如何在python中的散点图上过度绘制一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆