2013-02-03 9 views
0

필자는 회선 일반화 알고리즘을 가지고 있으며 플롯에 허용 오차를 증가시키는 스크롤 막대를 추가하려고합니다 (i는 더 많은 일반화를 이루었습니다). matplotlib를 사용하면 어떻게 가능합니까?Matplotlib 스크롤 막대

요약하면, 나는 줄에서 공차 효과의 증가를 표시 할 슬라이더를 클릭하고 드래그 할 수 있기를 원합니다.


여전히 정말로 고심하고 있습니다. 나는 단지 1-10의 간단한 척도로 하나의 슬라이더 만 원한다.


그래 데모는

fig = mp.figure() 
ax = fig.add_subplot(111) 
fig.subplots_adjust(left=0.25, bottom=0.25) 
min0=1 
max0=10 
tolerance = 0 

chain1 = ChainLoader('Wiggle1.txt') 
chain = chain1[0] 

chain2 = chain.generalise(tolerance) 

axcolor = 'lightgoldenrodyellow' 
axmin = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor) 
axmax = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor) 

tolerance = Slider(axmin, 'Min', 1, 10, valinit=min0) 
#smax = Slider(axmax, 'Max', 0, 30000, valinit=max0) 

def update(val): 
    tolerance = tolerance.val 
    #pp.show() 

tolerance.on_changed(update) 
#smax.on_changed(update) 
chain2 = chain.generalise(tolerance) 
pp.plotPolylines(chain2) 
pp.show() 

내 문제는 데프 업데이트 섹션을 작성하는 방법이다, 이것은 내가 지금까지 무엇을 가지고 난 그냥 일을 하나 개의 슬라이더를 얻을 struggerling있어, 도움이됩니다. 어떤 도움이 필요합니까?

from PointPlotter import PointPlotter 
from ChainHandler import ChainLoader 
pp=PointPlotter() 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider 

ax = plt.subplot(111) 
plt.subplots_adjust(left=0.25, bottom=0.25) 

tolerance = 0 
f0 = 0 
chain2 = ChainLoader('Wiggle1.txt') 
for chain in chain2: 

    chain2 = chain.generalise(tolerance) 
    pp.plotPolylines(chain2) 

axcolor = 'lightgoldenrodyellow' 

axtol = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor) 

tolerance = Slider(axtol, 'tol', 0.1, 30.0, valinit=f0) 

def update(val): 
    tolerance = tolerance.val 
    for chain in chain2: 

     chain2 = chain.generalise(tolerance) 
     pp.plotPolylines(chain2) 

     pp.plotPolylines(chain2) 

tolerance.on_changed(update) 

plt.show() 

너무 가까이! 그것의 플로팅,하지만 "UnboundLocalError : 지역 변수 '공차'이전에 할당 된 참조"스크롤 막대를 사용할 때 반환합니다. @tcaswell 어떤 도움?

+0

데모가 도움이됩니까? 코드에 _ 특이한 문제가 있습니까? – tacaswell

+0

수정 사항을 참조하십시오. 메모로, 만약 당신이 뭔가를 편집하고 그것에 대해 누군가의 관심을 끌고 싶다면, 주석에'@ 사용자 이름'을 넣으십시오 (예를 들어'@ tcaswell'은 나를 알릴 것입니다) – tacaswell

답변

0

slider 위젯 (doc)을 원합니다.

http://matplotlib.org/examples/widgets/slider_demo.html

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Slider, Button, RadioButtons 

ax = plt.subplot(111) 
plt.subplots_adjust(left=0.25, bottom=0.25) 
t = np.arange(0.0, 1.0, 0.001) 
a0 = 5 
f0 = 3 
s = a0*np.sin(2*np.pi*f0*t) 
l, = plt.plot(t,s, lw=2, color='red') 
plt.axis([0, 1, -10, 10]) 

axcolor = 'lightgoldenrodyellow' 
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor) 
axamp  = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor) 

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0) 
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0) 

def update(val): 
    amp = samp.val 
    freq = sfreq.val 
    l.set_ydata(amp*np.sin(2*np.pi*freq*t)) 
    plt.draw() 
sfreq.on_changed(update) 
samp.on_changed(update) 

resetax = plt.axes([0.8, 0.025, 0.1, 0.04]) 
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975') 
def reset(event): 
    sfreq.reset() 
    samp.reset() 
button.on_clicked(reset) 

rax = plt.axes([0.025, 0.5, 0.15, 0.15], axisbg=axcolor) 
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0) 
def colorfunc(label): 
    l.set_color(label) 
    plt.draw() 
radio.on_clicked(colorfunc) 

plt.show() 

이 사건이 적응하려면 :

#smax.on_changed(update) 
chain2 = chain.generalise(tol) 
pp.plotPolylines(chain2) 

def update(val): 
    tol = tolerance.val # get the value from the slider 
    chain2 = chain.generalise(tol) # shove that value into your code 
    ax.cla() # clear the axes 
    pp.plotPolylines(chain2) # re-plot your new results 

# register the call back 
tolerance.on_changed(update) 

이 변수 이름을 다시 사용에 대한주의 (여기

은 예로부터 데모입니다 float과 onc에 한 번, tolerance을 두 번 사용합니다. e는 Slider이고 python은 이전 변수를 완전히 다른 유형의 새로운 변수로 쉽게 삭제합니다).

update에서 나는 일반적으로 당신이 plotPolylines에 의해 반환되는 아티스트를 잡고 새 데이터들을 업데이트 할 그것을 그리기 다시 다음 axes 및 삭제, 대부분의 무차별 접근했다. (해당 단계에 대한 도움이 필요하면 데이터 구조에 대한 세부 정보가 포함 된 새 질문을 엽니 다.

.on_changed을 이해하는 방법은 슬라이더 사항이 변경되었을 때, 그것은 슬라이더의 현재 값 단일 인수 (val)와 당신 (update)에 전달 된 함수를 호출하는 것입니다. 그 함수 안에는 원하는 것을 할 수 있으며, 슬라이더가 바뀔 때마다 완전히 실행됩니다.

+0

당신을 도울 수 있습니까? 나는 이것을 끝내기에 아주 가깝다. 내가 얻게되는 오류는 "UnboundLocalError : 지역 변수 'tolerance'가 할당 전에 참조 됨" – DGraham