简体   繁体   English

将标签与从 NetworkX 图生成的散景图上的节点对齐

[英]Lining up labels with the nodes on a Bokeh figure generated from a NetworkX graph

I am trying to annotate a network graph which comes from NetworkX and which is visualised in Bokeh.我正在尝试注释来自 NetworkX 并在 Bokeh 中可视化的网络图。 I was able to successfully add the labels to the ColumnDataSource, and have them appear on the figure, but the coordinates appear to be wrong as the labels are not lined up with the nodes.我能够成功地将标签添加到 ColumnDataSource,并将它们显示在图中,但由于标签未与节点对齐,因此坐标似乎是错误的。 Any help would be greatly appreciated.任何帮助将不胜感激。

from bokeh.io import show
from bokeh.plotting import figure
from bokeh.models.graphs import from_networkx
from bokeh.models import ColumnDataSource, LabelSet


def visualise_graph(G):
    plot = figure(title="Title", tools="", x_range=(-1.5, 1.5),
              y_range=(-1.5, 1.5), toolbar_location=None)
    graph = from_networkx(G, nx.spring_layout)
    plot.renderers.append(graph)
    return plot


def prepare_labels(G, plot):
    pos = nx.spring_layout(G)
    x, y = zip(*pos.values())
    node_labels = nx.get_node_attributes(N, 'label')
    source = ColumnDataSource({'x': x, 'y': y,
                               'label': [node_labels[i] for i in range(len(x))]})
    labels = LabelSet(x='x', y='y', text='label', source=source,
                      background_fill_color='white')
    plot.renderers.append(labels)
    return plot

 plot = visualise_graph(N)
 plot_w_labels = prepare_labels(N, plot)
 show(plot_w_labels)

I discovered the problem which was that I was using nx.spring_layout() to get the coordinates which actually generates a new graph with new coordinates.我发现了一个问题,即我使用nx.spring_layout()来获取实际生成具有新坐标的新图形的坐标。 Instead I pulled the coordinates from the Bokeh figure using .layout_provider.graph_layout and it now works as desired.相反,我使用.layout_provider.graph_layout从散景图中提取坐标,现在它可以按需要工作。

My first ever post in stackoverflow so dont shoot me.我第一次在 stackoverflow 发帖,所以不要拍我。 Required change is smthg like this:所需的更改是这样的:

#draw graph with some layout
graph_renderer = from_networkx(graph_to_plot, nx.fruchterman_reingold_layout(graph_to_plot), scale=1, center=(0, 0))    
#x, y = zip(*pos.values())
x,y=zip(*graph_renderer.layout_provider.graph_layout.values())
graph_renderer.node_renderer.data_source.data['x']=x
graph_renderer.node_renderer.data_source.data['y']=y
.
.
label=LabelSet(x='x', y='y', text='index',level='glyph', source=graph_renderer.node_renderer.data_source)

@user2892709 - This is for you. @ user2892709 - 这是给你的。

First, extract the coordinates for the nodes using graph_renderer.layout_provider.graph_layout首先,使用graph_renderer.layout_provider.graph_layout提取节点的坐标

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

pos = graph_renderer.layout_provider.graph_layout
x,y=zip(*pos.values())

Then add those values to LabelSet()然后将这些值添加到LabelSet()

source = ColumnDataSource({'x':x,'y':y, 'field': <your_node_list>})
labels = LabelSet(x='x', y='y', text='field', source=source)

Finally, you can add the values to the graph using:最后,您可以使用以下方法将值添加到图形中:

plot.renderers.append(graph_renderer)
plot.renderers.append(labels)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM