How to resolve the clutter in my networkx graph using interactivity

依然范特西╮ 提交于 2021-01-29 17:10:40

问题


I am plotting a network graph of urls and applying louvain clustering. I could successfully plot the graph however it creates a clutter.

The code is:

pip install python-louvain
import community.community_louvain
partition = community.community_louvain.best_partition(G)
# draw the graph
pos = nx.spring_layout(G)
# color the nodes according to their partition
cmap = cm.get_cmap('viridis', max(partition.values()) + 1)
plt.figure(figsize=(15,15))
nx.draw_networkx_nodes(G, pos, partition.keys(), node_size=40,
               cmap=cmap, node_color=list(partition.values()))
nx.draw_networkx_edges(G, pos, alpha=0.5, with_labels = True)
nx.draw_networkx_label(G, pos=pos)
plt.show()

To have a better visual of the clutter produced:

values = [partition.get(node) for node in G.nodes()]

plt.figure(1, figsize=(20,20))
nx.draw_spring(G, cmap = plt.get_cmap('jet'), font_size = 8, node_color = values, node_size=70, 
                 with_labels=True)

This produces:

This made me think of ways to have interactive plots to make hover over nodes to plot url names. I though of using bokeh -

from bokeh.io import output_notebook, show, save
from bokeh.models import Range1d, Circle, ColumnDataSource, MultiLine
from bokeh.plotting import figure
from bokeh.models.graphs import from_networkx
#Choose a title!
title = 'RDFs'

#Establish which categories will appear when hovering over each node
HOVER_TOOLTIPS = [("Node", "@index")]

#Create a plot — set dimensions, toolbar, and title
plot = figure(tooltips = HOVER_TOOLTIPS,
          tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',
        x_range=Range1d(-10.1, 10.1), y_range=Range1d(-10.1, 10.1), title=title)

#Create a network graph object with spring layout
# https://networkx.github.io/documentation/networkx- 
1.9/reference/generated/networkx.drawing.layout.spring_layout.html
network_graph = from_networkx(G, nx.spring_layout, scale=10, center=(0, 0))

#Set node size and color
network_graph.node_renderer.glyph = Circle(size=15, fill_color=cmap(values))

#Set edge opacity and width
network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)

#Add network graph to the plot
plot.renderers.append(network_graph)

show(plot)
#save(plot, filename=f"{title}.html")

And It gives me an error.

ValueError: expected an element of either String, Dict(Enum('expr', 'field', 'value', 'transform'), Either(String, Instance(Transform), Instance(Expression), Color)) or Color, got [0, 0, 1, 1, 0, 0, 2, 2, 3, 3, 3, 4, 0, 0, 2, 2, 5, 0, 6, 2, 2, 2, 7, 6, 0, 2, 2, 2, 2, 8, 8, 2, 0, 9, 6, 1, 1, 0, 6, 0, 0, 8, 8, 7, 7, 3, 3, 2, 2, 8, 8, 7, 10, 10, 2, 2, 2, 2, 2, 2, 0, 0, 0, 2, 2, 2, 2, 3, 3, 0, 0, 9, 6, 0, 6, 0, 0, 7, 0, 8, 2, 2, 0, 0, 11, 11, 9, 8, 0, 9, 2, 7, 7, 1, 1, 0, 1, 1, 10, 10, 7, 4, 4, 10, 1, 11, 2, 2, 11, 1, 1, 0, 0, 3, 3, 0, 2, 0, 8, 8, 2, 0, 4, 6, 0, 0, 7, 3, 0, 0, 0, 4, 7, 2, 2, 7, 7, 11, 0, 0, 0, 0, 3, 9, 0, 6, 11, 0, 5, 9, 8, 11, 8, 11, 11, 0, 2, 2, 0, 0, 4, 7, 0, 1, 7, 0, 0, 5, 0, 7, 3, 4, 6, 3, 0, 2, 2, 7, 2, 9, 9, 0, 0, 2, 0, 0, 10, 4, 2, 3, 11, 8, 3, 2, 2, 8, 2, 4, 9, 9, 10, 10, 10, 2, 2, 0, 3, 3, 10, 0, 1, 11, 0, 11, 1, 0, 0, 4, 1, 8, 6, 8, 3, 7, 9, 1, 0, 0, 3, 8, 3, 2, 4, 3, 9, 9, 2, 8, 6, 1, 2, 11, 0, 0, 11, 4, 4, 2, 2, 4, 4, 5, 3, 0, 11, 7, 7, 10, 9, 11, 7, 9, 0, 9, 8, 0, 6, 0, 2, 5, 1, 3, 2, 2, 6, 1, 2, 7, 7, 1, 8, 1, 5, 5, 5, 11, 0, 0, 11, 2, 10, 7, 0, 2, 2, 0, 11, 2, 9, 9, 8, 0, 0, 2, 0, 9, 2, 4, 4, 6, 3, 7, 3, 6, 4, 4, 1, 2, 0, 2, 0, 0, 2, 7, 7, 7, 11, 2, 0, 6, 6, 4, 7, 2, 0, 9, 10, 3, 2, 4, 0, 0, 6, 8, 0, 8, 7, 7, 8, 2, 2, 2, 2, 2, 8, 0, 1, 10, 2, 9, 0, 0, 0, 2, 3, 3, 2, 2, 2, 11, 8, 2, 2, 6, 2, 4, 0, 5, 8, 9, 10, 10, 8, 8, 4, 1, 8, 0, 2, 2, 1, 2, 3, 6, 11, 0, 0, 2, 6, 2, 5, 4, 11, 2, 2, 6, 11, 2, 1, 4, 11, 0, 3, 11, 7, 7, 1, 1, 7, 4, 12, 1, 2, 9, 10, 1, 4, 8, 2, 1, 2, 5, 8, 2, 4, 11, 9, 7, 11, 3, 2, 3, 9, 1, 0, 0, 7, 4, 0, 0, 2, 11, 10, 2, 11, 11, 11, 11, 5, 2, 11, 0, 2, 11, 7, 2, 9, 8, 1, 1, 0, 11, 2, 2, 4, 7, 7, 2, 4, 4, 4, 2, 0, 0, 7, 2, 2, 9, 0, 1, 3, 0, 7, 8, 5, 1, 7, 5, 2, 0, 1, 2, 0, 6, 0, 0, 1, 8, 7, 2, 0, 2, 5, 2, 2, 2, 5, 1, 2, 4, 2, 11, 0, 0, 9, 5, 2, 6, 2, 0, 2, 11, 4, 7, 8, 2, 10, 1, 0, 2, 8, 7, 3, 0, 11, 2, 2, 7, 2, 2, 0, 3, 9, 0, 0, 0, 2, 0, 3, 2, 11, 10, 11, 0, 0, 2, 4, 0, 6, 2, 0, 1, 2, 4]

Can anyone please help me in using these cluster values for nodes to have colors in my interactive hover-over plot so that:

  1. Colors indicate clusters and
  2. Node labels are displayed over hover
  3. I also want to be able to increase the size of the plot overall. (Any ideas / recommendations regarding how to do that)

Any other visualization tool other than bokeh is also welcome. Thanks for your help

来源:https://stackoverflow.com/questions/65391943/how-to-resolve-the-clutter-in-my-networkx-graph-using-interactivity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!