Make Networkx Plot Look Nice
I would need to build a nice network using the following data: result_set = {('name1', 'job1'), ('name2', 'job2'), ('name3', 'job3'), ('name4', 'job4'), ('name5', 'job5'), ('name6'
Solution 1:
You can use one of the many layout algorithms implemented in networkx in nx.drawing.layout
to position the nodes in a way that makes the visualisation of the network easier. You can further adjust the distance between the nodes by setting k
to an appropriate value.
Also you can set the node size to be proportional to the degree by building a dict from Graph.degree
and setting the node_size
in nx.draw
accordingly and scaling it up to the desired size by applying a multiplicative factor. Here's an example using spring_layout
:
from pylab import rcParams
rcParams['figure.figsize'] = 14, 10
pos = nx.spring_layout(G, scale=20, k=3/np.sqrt(G.order()))
d = dict(G.degree)
nx.draw(G, pos, node_color='lightblue',
with_labels=True,
nodelist=d,
node_size=[d[k]*300for k in d])
You could also custom the node colour according to whether a node is a job
or a name
using the node_color
parameter (I'm guessing this is not the real case but it gives an idea of how to proceed):
rcParams['figure.figsize'] = 14, 10pos = nx.spring_layout(G, scale=20, k=3/np.sqrt(G.order()))
colors = [['lightgrey', 'lightblue'][node.startswith('job')]
for node in G.nodes()]
d = dict(G.degree)
nx.draw(G, pos,
with_labels=True,
nodelist=d,
node_size=[d[k]*300for k in d],
node_color=colors)
Post a Comment for "Make Networkx Plot Look Nice"