2017-11-09 3 views
4

다음 코드를 사용하여 대화식 bokeh 네트워크 그래프를 생성합니다. 노드 이름을 보케 플롯의 노드에 어떻게 추가합니까?bokeh 네트워크 그림에 노드 레이블 추가

from bokeh.io import show, output_notebook 
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool 
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes 
from bokeh.palettes import Spectral4 
from bokeh.models import LabelSet 

plot = Plot(plot_width=900, plot_height=500, 
      x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1)) 
plot.title.text = "Graph Interaction Demonstration" 

plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool()) 

graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0)) 

graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0]) 
graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2]) 
graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1]) 
graph_renderer.node_renderer.glyph.properties_with_values() 
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5) 
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=5) 
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=5) 

graph_renderer.selection_policy = NodesAndLinkedEdges() 
graph_renderer.inspection_policy = EdgesAndLinkedNodes() 

plot.renderers.append(graph_renderer) 

show(plot) 

결과 보케 networkx 그래프 : enter image description here

답변

1

화제! 나는 방금 가져 왔다고 생각해. 그들은 더 파이썬 방법이 될,하지만 내가 한 것은

  • 는 POS = nx.circular_layout (G)
  • 가이 위치를 추가를 사용하여 그래프에서 위치를 검색 관련 레이블 데이터 소스를 작성

    1. 이었다 있습니다 내
    2. 데이터 소스
    3. 이 위치에서 LabelSet 소스

    레오나르도 만들기, 당신은 G가 작성되는 라인을 누락되었습니다. 나는 karate_club_graph했다 이것은 나를 위해 작동 :

    from bokeh.models import ColumnDataSource 
    pos = nx.circular_layout(G) 
    x,y=zip(*pos.values()) 
    
    source = ColumnDataSource({'x':x,'y':y,'kid':['Joe %d' %ix for ix in range(len(x))]}) 
    labels = LabelSet(x='x', y='y', text='kid', source=source) 
    
    plot.renderers.append(labels) 
    
  • 1

    내가, 당신이 노드에 대한 대화 형 라벨을 의미 나는 또한하고 싶었던 일을 생각한다.

    hover = HoverTool(tooltips=[("Name:", "@name")]) 
    plot.add_tools(hover, TapTool(), BoxSelectTool(), WheelZoomTool()) 
    ... 
    graph_renderer.inspection_policy = NodesAndLinkedEdges() 
    

    그런 다음 노드에 대한 데이터 소스를 수정 :이를 위해, 당신은 몇 줄을 수정해야

    graph_renderer.node_renderer.data_source.data['name'] = [name1, ... ,nameN] 
    

    (데이터 소스가 이미 존재, 그것은 사전의있는 유일한에 대한 키는 번호가 매겨진 노드 목록 인 '인덱스'입니다. 따라서 이름 목록과 같은 길이의 목록을 참조하는 키를 추가 할 수 있습니다.이 목록은 '@key'를 통해 액세스 할 수 있습니다.

    +0

    문서에 있으십시오! 정말 고마워. –

    관련 문제