Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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小部件生成的绘图上使用的Matplotlib小部件_Python_Matplotlib_Widget_Matplotlib Widget - Fatal编程技术网

Python 在另一个Matplotlib小部件生成的绘图上使用的Matplotlib小部件

Python 在另一个Matplotlib小部件生成的绘图上使用的Matplotlib小部件,python,matplotlib,widget,matplotlib-widget,Python,Matplotlib,Widget,Matplotlib Widget,我试图使用matplotlib LassoSelector从散点图中选择一些点,并仅为选定点生成单独的图形。当我尝试在第二个绘图上使用另一个matplotlib小部件时,它不起作用,但没有错误或警告消息。下面是一个使用LassoSelector和SpanSelector的最小示例 我也尝试了其他小部件;按钮小部件显示按钮,但不执行按钮按下操作 import numpy as np from matplotlib.pyplot import * from matplotlib.widgets im

我试图使用matplotlib LassoSelector从散点图中选择一些点,并仅为选定点生成单独的图形。当我尝试在第二个绘图上使用另一个matplotlib小部件时,它不起作用,但没有错误或警告消息。下面是一个使用LassoSelector和SpanSelector的最小示例

我也尝试了其他小部件;按钮小部件显示按钮,但不执行按钮按下操作

import numpy as np
from matplotlib.pyplot import *
from matplotlib.widgets import SpanSelector, LassoSelector
from matplotlib.path import Path

def onselect(verts):
    global xys,data

    #get indexes of selected points
    path = Path(verts)
    xysn = xys.get_offsets()
    ind = np.nonzero([path.contains_point(xy) for xy in xysn])[0]

    #plot the second figure
    fig=figure(2)
    ax=fig.add_subplot(111)
    ax.hist(data[:,0][ind],10)

    #this should be executed when SpanSelector is used
    def action(min,max):
        print min,max

    #try to do SpanSelector (this fails)
    span=SpanSelector(ax,action,'horizontal')

    show()

#initialize a figure
fig=figure(1)
ax=fig.add_subplot(111)

#create data
data=np.array([[1,6], [4,8],[0,4],[4,2],[9,6],[10,8],[2,2],[5,5],[0,4],[4,5]])

#plot data
xys=ax.scatter(data[:,0],data[:,1])

#select point by drawing a path around them
lasso = LassoSelector(ax, onselect=onselect)

show()

matplotlib
小部件是事件驱动的,因此等待用户输入。代码的问题在于,您试图使用新的事件处理程序
SpanSelector
创建一个新图形。我不确定您是否可以根据以前的事件添加新的事件,如果
SpanSelector
被注释掉,我会得到以下错误

QCoreApplication::exec: The event loop is already running
因此,新事件,
LassoSelector
未注册,用户输入未被拾取(并且新图形未出现)。最好在代码开头创建所有图形并注册所有可能的事件。以下内容应该更接近您想要做的事情

import numpy as np
from matplotlib.pyplot import *
from matplotlib.widgets import SpanSelector, LassoSelector
from matplotlib.path import Path

#this should be executed when LassoSelector is used
def onselect(verts):
    global xys,data

    #get indexes of selected points
    path = Path(verts)
    xysn = xys.get_offsets()
    ind = np.nonzero([path.contains_point(xy) for xy in xysn])[0]

    #Clear and update bar chart
    h, b = np.histogram(data[:,0][ind],10)
    for rect, bars in zip(rects, h):
        rect.set_height(bars)
    ax2.bar(mb, h, align='center')
    draw()

#this should be executed when SpanSelector is used
def action(min,max):
    print min,max

#initialize figures
fig1=figure(1)
ax1=fig1.add_subplot(111)

fig2=figure(2)
ax2=fig2.add_subplot(111)

#create data
data=np.array([[1,6],[4,8],[0,4],[4,2],[9,6],[10,8],[2,2],[5,5],[0,4],[4,5]])

#plot data
xys=ax1.scatter(data[:,0],data[:,1])

#Plot initial histogram of all data
h, b = np.histogram(data[:,0],10)
mb = [0.5*(b[i]+b[i+1]) for i in range(b.shape[0]-1)]
rects = ax2.bar(mb, h, align='center')

#Register lasso selector
lasso = LassoSelector(ax1, onselect=onselect)

#Register SpanSelector 
span=SpanSelector(ax2,action,'horizontal')

show()
注意,为了更新条形图,它比曲线图要复杂一些,所以我在这里使用了这个答案

出于某种原因,直方图图2仅在单击时更新。我会考虑使用一个具有两个轴的单个图形,这可能更容易使用,

fig, ax = subplots(2,1)
ax1 = ax[0]; ax2 = ax[1]