首页 > 解决方案 > 如何在一张图中绘制多个图的线性拟合?

问题描述

我正在尝试为同一图中的三个图绘制线性最佳拟合线,以及它们各自的斜率和截距;我不知道该怎么做,所以它们都在我的数据中的同一张图中。有谁知道如何做到这一点?我附上了一张图表在下面的样子。

CSVfile_1 = 'Gas01_Fast.csv'
V_1, T_1, P_1, t_1 = getVTPt(filename='Gas01_Fast.csv')

CSVfile_2 = 'Gas02_Fast.csv'
V_2, T_2, P_2, t_2 = getVTPt(filename='Gas02_Fast.csv')

CSVfile_3 = 'Gas03_Fast.csv'
V_3, T_3, P_3, t_3 = getVTPt(filename='Gas03_Fast.csv')


fig, ax = plt.subplots()

ax.set_xlabel("Log(Volume)")
ax.set_ylabel("Log(Pressure)")
ax.set_title("Log(Pressure) vs Log(Volume)")


ax.errorbar(x=np.log10(V_1),y=np.log10(P_1),xerr=0,yerr=0,fmt='ro', ms=3)

ax.errorbar(x=np.log10(V_2),y=np.log10(P_2),xerr=0,yerr=0, fmt='bo', ms=3)

ax.errorbar(x=np.log10(V_3),y=np.log10(P_3),xerr=0,yerr=0, fmt='go', ms=3)

plt.legend(["Gas01_Fast.csv", "Gas02_Fast.csv","Gas03_Fast.csv"])

plt.show()

上面代码生成的图形图像

标签: pythonnumpycsvmatplotlib

解决方案


您需要指定不同于 0 的 xerr 和 yerr。它们指定您希望沿 x 和 y 轴的误差条大小为多长时间。

您也不必使用 subplots 和 ax 而是可以直接在 plt 上调用 errorbar ,就像在此处的示例中一样:

https://matplotlib.org/3.3.2/gallery/lines_bars_and_markers/errorbar_limits_simple.html


推荐阅读