Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib:如何跨多个小部件工作?如何正确地阻止它们?_Python_Matplotlib_Widget_Break - Fatal编程技术网

Python Matplotlib:如何跨多个小部件工作?如何正确地阻止它们?

Python Matplotlib:如何跨多个小部件工作?如何正确地阻止它们?,python,matplotlib,widget,break,Python,Matplotlib,Widget,Break,我想知道是否有人可以在matplotlib小部件的应用程序中帮助我解决这个问题: 我必须研究HII星系的光谱(这些只是一个平面图,有一些突然出现的峰值,我们知道它们的位置)。我需要“确认”这些线的位置(你可以说曲线图上峰的厚度)并确定连续性水平(这意味着曲线图在峰的左右区域的平均y坐标)。这是一个简单的任务,但是考虑到绘图的数量和每个绘图中的行数,我想使用一个简短的python代码来简化这个过程,避免遗漏一些峰值 我已经在youtube上观看了John Hunter的讲座,我相信我会非常乐意使用

我想知道是否有人可以在matplotlib小部件的应用程序中帮助我解决这个问题:

我必须研究HII星系的光谱(这些只是一个平面图,有一些突然出现的峰值,我们知道它们的位置)。我需要“确认”这些线的位置(你可以说曲线图上峰的厚度)并确定连续性水平(这意味着曲线图在峰的左右区域的平均y坐标)。这是一个简单的任务,但是考虑到绘图的数量和每个绘图中的行数,我想使用一个简短的python代码来简化这个过程,避免遗漏一些峰值

我已经在youtube上观看了John Hunter的讲座,我相信我会非常乐意使用默认的小部件作为我的GUI,特别是spanselector、press事件和cursor事件

我的问题是,我不确定使用影响同一图形的多个小部件时,哪种方法是正确的:目前,我的代码打开了一个给定的频谱,并开始显示我知道第一行应该在哪里的部分绘图。然后,它应该继续显示下一行。在每个循环中,它应该执行如下操作:(它是matplotlib库中的spanselector示例的副本,适用于此示例)

首先,此代码有两个问题:

  • 绘图将跳转到最终跨距选择,忽略两个跨距 先前的
  • 代码不会停止。在Spani的所有示例中 已经发现,代码一直在运行。。。我找不到停止的方法 这样我就可以继续循环了
  • 最后,(但不太重要)每个区域填充的修改 在每个区域选择后,绘图不会很好地“记住”这些区域
  • 我的第一个问题是如何避免这些问题。。。我还有其他疑问:

    • A) 我想我可以通过显示和关闭图表来实现这一点 在每个量程选择器之前。。。但那会破坏我的工作流程
    • B) 我可以用关键事件来处理这个问题吗?怎么用?应该是单身吗 在代码中前进的键?我们的代码应该是一个单独的代码 我要应用的每个小部件
    • C) 你能给我一些关于matplotlib的教程吗 要使用关键事件管理代码流
    如果你能读到这个很长的问题,非常感谢

    ---编辑

    上周我一直在研究这个问题,我终于开始了解它应该如何工作:

    1) 事件和plt.show必须始终运行。事实上,后者被设计为在代码末尾实现。事件调用应该正好在

    2) 要更改图形上的数据,可以使用关键事件。这篇文章说明了正确的方法,并为它提供了一个类

    3) 这种结构的问题在于,您的代码必须以不同的方法分段。您需要确保在主代码中定义变量,以便可以跨不同的方法导入它们(我仍在努力解决这个问题…完成后,我将发布一个示例代码)

    还有一个问题:当我尝试使用spanselector小部件运行游标小部件时,小部件会以一种相当。。。痛苦的方式到眼睛的方式。。。有没有办法避免这种情况?我一直在玩游标useblit小部件,但运气不好

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.widgets import SpanSelector
    
    def onselectPeak(xmin, xmax):
            indmin, indmax = np.searchsorted(x, (xmin, xmax))
            indmax = min(len(x)-1, indmax)
            print "Xmin " + str(indmin) + " at index " + str(indmin)
            print "Xmax " + str(indmax) + " at index " + str(indmax)
            ax.fill_between(x[indmin:indmax], -1.0, y[indmin:indmax],facecolor='Red',alpha=0.5)
    
    def onselectLeftContinuum(xmin, xmax):
            indmin, indmax = np.searchsorted(x, (xmin, xmax))
            indmax = min(len(x)-1, indmax)      
            print "Leftmin " + str(indmin) + " at index " + str(indmin)
            print "Leftmax " + str(indmax) + " at index " + str(indmax)
            ax.fill_between(x[indmin:indmax], -1.0, y[indmin:indmax],facecolor='Blue',alpha=0.5)
    
    def onselectRightContinuum(xmin, xmax):
            indmin, indmax = np.searchsorted(x, (xmin, xmax))
            indmax = min(len(x)-1, indmax)
            print "Xmin " + str(indmin) + " at index " + str(indmin)
            print "Xmax " + str(indmax) + " at index " + str(indmax)
            ax.fill_between(x[indmin:indmax], -1.0, y[indmin:indmax],facecolor='Blue',alpha=0.5)       
    
    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(111, axisbg='#FFFFCC')
    
    x = np.arange(0.0, 10.0, 1.0)
    y = [0.0,0.0,0.0,0.0,5.0,0.0,0.0,0.0,0.0,0.0,]
    
    ax.plot(x, y, '-')
    ax.set_ylim([-1.0,6.0])
    
    ax.set_title('Press left mouse button and drag Line region')
    span = SpanSelector(ax, onselectPeak, 'horizontal', useblit=True, rectprops=dict(alpha=0.5, facecolor='red') )
    
    ax.set_title('Press left mouse button and drag left region of the continuum')
    span = SpanSelector(ax, onselectLeftContinuum, 'horizontal', useblit=True, rectprops=dict(alpha=0.5, facecolor='blue') )
    
    ax.set_title('Press left mouse button and drag right region of the continuum')
    span = SpanSelector(ax, onselectRightContinuum, 'horizontal', useblit=True, rectprops=dict(alpha=0.5, facecolor='blue') )
    
    plt.show()
    
    print "Task Completed"