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

Python 如何在matplotlib中的范围之间绘制数据?,python,matplotlib,plot,scipy,range,Python,Matplotlib,Plot,Scipy,Range,我使用curve_fit函数将曲线拟合到数据,但只需要拟合两点之间的曲线。我的意思不是改变轴的长度,而是改变绘制数据的范围。例如,在下图中,我只需要在x=9和x=11之间绘制蓝线。我该怎么做 我已经得到的代码是: #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.

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

我已经得到的代码是:

#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)
#绘图数据
plt.图(kcoord,数据[4],'k',alpha=0.25)
#第5栏
y2=数据[4]
x2=kcoord
#获取所需范围内元素的索引
idx2=np.argwhere((9
一旦你有了
x
y
来绘图,你就可以简单地做
plt.plot(x[(x>=x_min)&(x=x_min)&(x