【问题标题】:How to slice list from matplotlib ginput如何从 matplotlib ginput 中切片列表
【发布时间】:2017-08-07 14:06:54
【问题描述】:

我在 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)

这给了我一个数组,但它没有在我的图表上绘制这些值 - 有人对我做错了什么有任何意见吗?

谢谢

【问题讨论】:

    标签: python arrays python-3.x matplotlib ginput


    【解决方案1】:

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

    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()
    

    【讨论】:

      猜你喜欢
      • 2015-08-25
      • 2013-01-28
      • 2017-05-15
      • 1970-01-01
      • 2010-12-14
      • 2015-02-04
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      相关资源
      最近更新 更多