Reputation:
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', 'job6'), ('name7', 'job7'), ('name8', 'job8'), ('name9', 'job3'), ('name10', 'job6'), ('name11', 'job3'), ('name12', 'job1'), ('name13', 'job5'), ('name14', 'job9'), ('name15', 'job10'), ('name16', 'job6'), ('name17', 'job7'), ('name18', 'job11'), ('name19', 'job12'), ('name20', 'job13'), ('name21', 'job7'), ('name22', 'job14'), ('name23', 'job15'), ('name24', 'job7'), ('name25', 'job14'), ('name26', 'job9'), ('name27', 'job3'), ('name28', 'job16'), ('name29', 'job16'), ('name30', 'job1'), ('name31', 'job10'), ('name32', 'job9'), ('name33', 'job12'), ('name34', 'job5'), ('name35', 'job7'), ('name36', 'job3'), ('name37', 'job17'), ('name38', 'job3'), ('name39', 'job18'), ('name40', 'job16 / job3'), ('name41', 'Il Foglio'), ('name42', 'job7'), ('name43', 'job19'), ('name44', 'job9'), ('name45', 'job20'), ('name46', 'job18'), ('name47', 'job21')}
Names are unique, but not jobs, as you can see. I would like, therefore, to build a network that can show clusters of names by jobs.
I did it using the following code, but I am having some issues to visualise in a nice format, without overlapping the labels and changing nodes size depending on degree.
My code is the following:
result = zip(names, jobs)
# Converting itertor to set
result_set = set(result)
print(result_set)
G = nx.Graph()
for node_tuple in result_set:
G.add_edges_from(result_set) # edited after comment
nx.draw(G, with_labels=True)
plt.show()
Could you please have a look at it and tell me how I could change it accordingly to let it better readable and show nodes depending on nodes' degree?
Suggestions and comments are always welcomed.
Upvotes: 6
Views: 6086
Reputation: 88305
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]*300 for 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, 10
pos = 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]*300 for k in d],
node_color=colors)
Upvotes: 3