Python散点图绘制拟合曲线教程:一步一步实现
当然可以!以下是使用Python绘制散点图并拟合曲线的示例代码:
import numpy as np
import matplotlib.pyplot as plt
# 生成随机散点数据
x = np.linspace(0, 10, 100)
y = 2 * x + np.random.randn(100)
# 绘制散点图
plt.scatter(x, y, label='Data Points')
# 使用numpy的polyfit函数拟合一次多项式曲线
coefficients = np.polyfit(x, y, 1)
fitted_line = np.polyval(coefficients, x)
# 绘制拟合曲线
plt.plot(x, fitted_line, color='red', label='Fitted Line')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter Plot with Fitted Line')
plt.legend()
plt.grid(True)
plt.show()
希望这可以帮到你!如果你有更多关于Python或其他方面的问题,请随时提问。
原文地址: https://www.cveoy.top/t/topic/Rl6 著作权归作者所有。请勿转载和采集!