2017-09-29 12 views
0

나는 Bokeh를 처음 사용합니다. 체크 박스를 클릭하면 보케 그림에 선을 추가/삭제할 수 있기를 원하는 위젯을 만들었습니다. 나는 20 개의 그런 체크 박스를 가지고 있는데 체크 박스가 선택되지 않았다면 전체 라인을 지우고 싶지 않다.보케의 그림에서 줄을 삭제

이것은 그림 개체에 대한 액세스 권한이있는 콜백을 통해 수행됩니다. 나는 이런 식으로 뭔가 할 수있는 방법이 상상 : 삭제) 문자 모양을

F=figure() 
F.line('x', 'y', source=source, name='line1') 
F.line('x', 'z', source=source, name='line2') 

%%in callback 
selected_line_name = 'line1' # this would be determined by checkbox 
selected_line = F.children[selected_line_name] 
delete(selected_line) 

그러나, 나는 그것의 부모 개체 2에서 글리프에 액세스하는 방법 1) 알아낼 수없는 오전

나는 소스 'y'를 = []로 설정했지만, 모든 컬럼 데이터 소스가 같은 크기 일 필요하기 때문에,이 모든 음모를 ... 제거

에는 여러 가지가 있습니다

답변

2

:

# Keep the glyphs in a variable: 
line2 = F.line('x', 'z', source=source, name='line2') 

# or get the glyph from the Figure: 
line2 = F.select_one({'name': 'line2'}) 

# in callback: 
line2.visible = False 
+0

감사 @ 알렉스, 이것은 위대하다! 라인을 숨기지 않는 방법이 있습니까? (메모리에 저장하고 시스템 속도를 늦추지 만) 완전히 삭제하려면 어떻게해야합니까? – DankMasterDan

+0

나는 이것을 추측한다 :'F.renderers.remove (line2)'(렌더러는 목록으로 동작하여 파이썬 목록으로 조작 할 수 있습니다.)하지만 데이터가 소스에 저장 될 때 최적화가 최소화된다고 생각합니다. 그래서 그 글리프를 파싱하고 가시성을 점검 할 필요가없는 보케에만 저장됩니다. 결코 내부 구조에 익숙하지 않은 채로 프로파일 링하지 마십시오. 당신이 그것을 프로파일한다면, 저희에게 알려주십시오! – Alex

+0

감사합니다! 그래프의 모든 선을 자동으로 삭제하거나 재설정 할 수 있습니까? – DankMasterDan

1

글리프가 변수로 지정되고 이름 속성이 지정된 경우 공유 'x'데이터 소스 열을 유지 관리합니다. remove 함수는 해당 'y'열을 nans로 채우고 restore 함수는 nans를 원래 값으로 바꿉니다.

이 함수에는 numpy 및 bokeh GlyphRenderer 가져 오기가 필요합니다. 나는이 메소드가 단순한 가시적 인 on/off 옵션을 감안할 때 가치가 있는지 확신하지 못한다. 그러나 어쨌든이 메소드는 다른 사용 사례에서 도움이 될 수 있도록 게시하고있다.

제거 또는 복원 할 글리프는 목록에 포함 된 글리프 이름으로 참조됩니다.

src_dict = source.data.copy() 

def remove_glyphs(figure, glyph_name_list): 
    renderers = figure.select(dict(type=GlyphRenderer)) 
    for r in renderers: 
     if r.name in glyph_name_list: 
      col = r.glyph.y 
      r.data_source.data[col] = [np.nan] * len(r.data_source.data[col]) 

def restore_glyphs(figure, src_dict, glyph_name_list): 
    renderers = figure.select(dict(type=GlyphRenderer)) 
    for r in renderers: 
     if r.name in glyph_name_list: 
      col = r.glyph.y 
      r.data_source.data[col] = src_dict[col] 

예 :

from bokeh.plotting import figure, show 
from bokeh.io import output_notebook 
from bokeh.models import Range1d, ColumnDataSource 
from bokeh.models.renderers import GlyphRenderer 

import numpy as np 

output_notebook() 

p = figure(plot_width=200, plot_height=150, 
      x_range=Range1d(0, 6), 
      y_range=Range1d(0, 10), 
      toolbar_location=None) 

source = ColumnDataSource(data=dict(x=[1, 3, 5], 
            y1=[1, 1, 2], 
            y2=[1, 2, 6], 
            y3=[1, 3, 9])) 

src_dict = source.data.copy() 

line1 = p.line('x', 'y1', 
       source=source, 
       color='blue', 
       name='g1', 
       line_width=3) 

line2 = p.line('x', 'y2', 
       source=source, 
       color='red', 
       name='g2', 
       line_width=3) 

line3 = p.line('x', 'y3', 
       source=source, 
       color='green', 
       name='g3', 
       line_width=3) 
print(source.data) 
show(p) 

아웃 :

{'x': [1, 3, 5], 'y1': [1, 1, 2], 'y2': [1, 2, 6], 'y3': [1, 3, 9]} 

enter image description here

remove_glyphs(p, ['g1', 'g2']) 
print(source.data) 
show(p) 

아웃 :

restore_glyphs(p, src_dict, ['g1', 'g3']) 
print(source.data) 
show(p) 

23,

enter image description here는 ('G3'는 음모에 이미, 그리고 영향을받지 않습니다)

아웃 :

{'x': [1, 3, 5], 'y1': [1, 1, 2], 'y2': [nan, nan, nan], 'y3': [1, 3, 9]} 

enter image description here

restore_glyphs(p, src_dict, ['g2']) 
print(source.data) 
show(p) 
아웃 :

{'x': [1, 3, 5], 'y1': [1, 1, 2], 'y2': [1, 2, 6], 'y3': [1, 3, 9]} 

enter image description here