2016-07-22 5 views
0

Jupyter Notebook을 사용하여 ipywidgets를 사용하여 유사한 플롯을 효과적으로 표시하려면 어떻게해야합니까?ipywidgets를 사용하여 플롯의 요소를 효율적으로 대체하는 방법은 무엇입니까?

무거운 플롯 (많은 양의 데이터 포인트가 있고 그것을 플로팅하는 데 많은 시간이 걸린다)을 대화식으로 플롯하고 모든 복잡한 플롯을 다시 채우지 않고 ipywidgets에서 상호 작용을 사용하여 단일 요소를 수정하고자합니다. 이것을하기위한 내장 기능이 있습니까?

는 기본적으로 난 할 노력하고있어 지금은 각 replot 2 초 정도 소요

import numpy as np 
import matplotlib.pyplot as plt 
from ipywidgets import interact 
import matplotlib.patches as patches 
%matplotlib inline #ideally nbagg 

def complicated plot(t): 
    plt.plot(HEAVY_DATA_SET) 
    ax = plt.gca() 
    p = patches.Rectangle(something_that_depends_on_t) 
    ax.add_patch(p) 

interact(complicatedplot, t=(1, 100)); 

입니다. 거기에 그림을 유지하고 그 직사각형을 바꿀 수있는 방법이있을 것으로 기대합니다.

해킹은 상수 부분의 그림을 만들고, 줄거리를 배경으로 만들고, 사각형 부분을 플롯합니다. 하지만

너무 더러운 소리 (나는 당신이 IPython 또는 Jupyter 노트북에 있으리라 믿고있어)이 사각형의 너비를 변경하는 대화 형 방법의 거친 예를 들어 당신

+0

정말하고 싶은 것에 대한 자세한 내용을 추가 할 수 있습니까? 예를 들어 스 캐터 플롯에서 플롯이'p' 변수에서 참조되는 경우'p.set_offsets'를 사용하여 데이터를 다시 정의 할 수 있습니다. 어쩌면 당신은'ax.get_children()'과 같은 것을 할 수 있고 그 중 하나를 수정할 수 있습니다. 그리고 위젯의 경우 ipywidgets에 함수를 정의하여 함수를 정의 할 수있는'observe' 메서드가 있다고 생각합니다. –

+0

100 개 라인을 표시하려고합니다 (plt.plot (HEAVY_DATA_SET)) 그리고 그 라인들 위에 수직선을 추가하면된다. – gota

+0

아마'ax.get_children()'에서 사각형을 제거하고 그 사각형을 다시 그릴 수 있을까? 예를 들어,'ax.get_children()'을 호출하면리스트에'

답변

1

감사합니다

import matplotlib 
import matplotlib.pyplot as plt 
import matplotlib.patches as patches 

import ipywidgets 
from IPython.display import display 

%matplotlib nbagg 

f = plt.figure() 
ax = plt.gca() 

ax.add_patch(
    patches.Rectangle(
     (0.1, 0.1), # (x,y) 
     0.5,   # width 
     0.5,   # height 
    ) 
) 

# There must be an easier way to reference the rectangle 
rect = ax.get_children()[0] 

# Create a slider widget 
my_widget = ipywidgets.FloatSlider(value=0.5, min=0.1, max=1, step=0.1, description=('Slider')) 

# This function will be called when the slider changes 
# It takes the current value of the slider 
def change_rectangle_width(): 
    rect.set_width(my_widget.value) 
    plt.draw() 

# Now define what is called when the slider changes 
my_widget.on_trait_change(change_rectangle_width) 

# Show the slider 
display(my_widget) 

그런 다음 슬라이더를 움직이면 직사각형의 너비가 변경됩니다. 나는 코드를 정돈하려고 노력할 것이다. 그러나 당신은 그 생각을 가지고 있을지도 모른다. 좌표를 변경하려면 rect.xy = (x0, y0)을 수행해야합니다. 여기서 x0y0은 새 좌표입니다.

관련 문제