首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从matplotlib ginput中切片列表

如何从matplotlib ginput中切片列表
EN

Stack Overflow用户
提问于 2017-03-16 19:56:20
回答 1查看 575关注 0票数 2

我在Python中有一个值列表,我正在用matplotlib绘制它。然后,我尝试在matplotlib中使用ginput来单击图上的两个点,将从这两个点获取X坐标,在这两个点之间对我的原始列表进行切片。然而,我似乎找不到这样做的方法。

我已经有一个名为MIList的数字列表,下面的代码对我不起作用:

代码语言:javascript
复制
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)

这给了我一个数组,但它没有在我的图上绘制这些值-有人知道我做错了什么吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-16 21:49:46

主要的一点是,一旦对画布进行了更改,就需要重新绘制画布。因此,为了使新绘图变得可见,您可以调用

代码语言:javascript
复制
plt.gcf().canvas.draw()

下面是一个完整的工作代码:

代码语言:javascript
复制
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()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42833413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档