我在Python中有一个值列表,我正在用matplotlib绘制它。然后,我尝试在matplotlib中使用ginput来单击图上的两个点,将从这两个点获取X坐标,在这两个点之间对我的原始列表进行切片。然而,我似乎找不到这样做的方法。
我已经有一个名为MIList的数字列表,下面的代码对我不起作用:
startinput = plt.ginput(2)
print("clicked", startinput)
startinputxvalues = [x[0] for x in startinput]
print(startinputxvalues)
x1 = startinputxvalues[0]
print(x1)
x2 = startinputxvalues[1]
print(x2)
slicedMIList = [MIList[int(x1):int(x2)]]
plt.plot(slicedMIList)这给了我一个数组,但它没有在我的图上绘制这些值-有人知道我做错了什么吗?
谢谢
发布于 2017-03-16 21:49:46
主要的一点是,一旦对画布进行了更改,就需要重新绘制画布。因此,为了使新绘图变得可见,您可以调用
plt.gcf().canvas.draw()下面是一个完整的工作代码:
import matplotlib.pyplot as plt
import numpy as np
X = np.arange(10)
Y = np.sin(X)
plt.plot(X, Y)
startinput = plt.ginput(2)
x, y = zip(*startinput)
Ysliced = Y[int(x[0]):int(x[1])+1]
Xsliced = X[int(x[0]):int(x[1])+1]
plt.plot(Xsliced, Ysliced, color="C3", linewidth=3)
#draw the canvas, such that the new plot becomes visible
plt.gcf().canvas.draw()
plt.show()

https://stackoverflow.com/questions/42833413
复制相似问题