2016-07-05 2 views
2

matplotlib LassoSelector를 사용하여 산점도에서 일부 포인트를 선택하고 선택된 포인트에 대해서만 별도의 그림을 생성하려고합니다. 두 번째 플롯에서 다른 matplotlib 위젯을 사용하려고하면 작동하지 않지만 오류나 경고 메시지는 없습니다. 아래는 LassoSelector와 SpanSelector가 사용 된 최소 예제입니다.Matplotlib 위젯이 다른 matplotlib 위젯에 의해 생성 된 플롯에 사용되었습니다

다른 위젯을 사용해 보았습니다. 버튼 위젯은 버튼을 표시하지만 버튼 누름에 대한 작업은 수행되지 않습니다.

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

답변

0

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

주, 막대 차트를 업데이트하기 위해, 조금 플롯보다 더 까다로운 그래서 나는이 대답은 어떤 이유로 여기 Dynamically updating a bar plot in matplotlib

사용, 당신이 원하는 무엇을 가까이해야합니다 히스토그램 그림 2는 클릭 할 때만 업데이트됩니다. 이를 위해 두 개의 축이있는 단일 그림을 사용하는 것이 더 간편 할 수 있습니다.

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