Python Matplotlib:如何跨多个小部件工作?如何正确地阻止它们?
我想知道是否有人可以在matplotlib小部件的应用程序中帮助我解决这个问题: 我必须研究HII星系的光谱(这些只是一个平面图,有一些突然出现的峰值,我们知道它们的位置)。我需要“确认”这些线的位置(你可以说曲线图上峰的厚度)并确定连续性水平(这意味着曲线图在峰的左右区域的平均y坐标)。这是一个简单的任务,但是考虑到绘图的数量和每个绘图中的行数,我想使用一个简短的python代码来简化这个过程,避免遗漏一些峰值 我已经在youtube上观看了John Hunter的讲座,我相信我会非常乐意使用默认的小部件作为我的GUI,特别是spanselector、press事件和cursor事件 我的问题是,我不确定使用影响同一图形的多个小部件时,哪种方法是正确的:目前,我的代码打开了一个给定的频谱,并开始显示我知道第一行应该在哪里的部分绘图。然后,它应该继续显示下一行。在每个循环中,它应该执行如下操作:(它是matplotlib库中的spanselector示例的副本,适用于此示例) 首先,此代码有两个问题:Python Matplotlib:如何跨多个小部件工作?如何正确地阻止它们?,python,matplotlib,widget,break,Python,Matplotlib,Widget,Break,我想知道是否有人可以在matplotlib小部件的应用程序中帮助我解决这个问题: 我必须研究HII星系的光谱(这些只是一个平面图,有一些突然出现的峰值,我们知道它们的位置)。我需要“确认”这些线的位置(你可以说曲线图上峰的厚度)并确定连续性水平(这意味着曲线图在峰的左右区域的平均y坐标)。这是一个简单的任务,但是考虑到绘图的数量和每个绘图中的行数,我想使用一个简短的python代码来简化这个过程,避免遗漏一些峰值 我已经在youtube上观看了John Hunter的讲座,我相信我会非常乐意使用
- A) 我想我可以通过显示和关闭图表来实现这一点 在每个量程选择器之前。。。但那会破坏我的工作流程
- B) 我可以用关键事件来处理这个问题吗?怎么用?应该是单身吗 在代码中前进的键?我们的代码应该是一个单独的代码 我要应用的每个小部件
- C) 你能给我一些关于matplotlib的教程吗 要使用关键事件管理代码流
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"