简体   繁体   中英

How to display a network with better separation using the Networkx package?

I created a graph object using the pytextrank package like so:

import pytextrank
### ........................... 
### Some steps and calculations
### ...........................
graph, ranks = pytextrank.text_rank(path_stage1)

And I can use the networkx package to make a networkx drawing like so:

import networkx as nx
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(50,50))
nx.draw(graph, with_labels=True) 

plt.show()

在此处输入图片说明

But as you can see, the words are mostly concentrated around the center. I would like to see higher separation between classes of words. How do I do that?

What you are looking for is a different layout (Networkx's term for node positioning algorithms that are used when drawing graphs). By default, nx.draw() is using spring layout. Changing your layout can be done as follows:

import networkx as nx
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(50,50))
pos = nx.spectral_layout(graph)
nx.draw(graph, pos=pos, with_labels=True) 

plt.show()

You can play with a few layouts to find one that suits your needs.

Another option is to save your graph to a file, and load it with gephi , which has a nice GUI for visualizing and exploring your graph. (Also supporting several graph layout algorithms).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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