微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在matplotlib中的范围之间绘制数据?

如何解决如何在matplotlib中的范围之间绘制数据?

我使用 curve_fit 函数为数据拟合了一条曲线,但只需要 2 个点之间的拟合曲线。我的意思不是更改轴长度,而是更改绘制数据的范围。例如在下图中,我只需要在 x=9 和 x=11 之间绘制蓝线。我该怎么做?

enter image description here

我已经得到的代码

#plotting data
plt.plot(kcoord,data[4],'k',alpha=0.25)

#column 5
y2=data[4]
x2=kcoord
# get indices of elements in desired range
idx2 = np.argwhere((9 < x2) & (x2 < 11))
# get y-values in desired range
selection2 = y2[idx2]
xshort2=x2[idx2]
plt.plot(xshort2,selection2)


#column 5 curvefitting
init_guess2=np.array([-0.31934191,6.23228567,-31.04172149])
popt2,pcov2 = curve_fit(func,xshort2.ravel(),selection2.ravel(),init_guess2)
plt.plot(x2,func(x2,*popt2),label="quadratic fit",color='blue')
print(popt2)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。