Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import networkx as nx
import pandas as pd
import numpy as np
from fa2 import ForceAtlas2
def get_force_atlas(weight_influence=0, scaling_ratio=3.0, gravity=5):
forceatlas2 = ForceAtlas2(
# Behavior alternatives
outboundAttractionDistribution=True, # Dissuade hubs
linLogMode=False, # NOT IMPLEMENTED
adjustSizes=False, # Prevent overlap (NOT IMPLEMENTED)
edgeWeightInfluence=weight_influence,
# Performance
jitterTolerance=1.0, # Tolerance
barnesHutOptimize=True,
barnesHutTheta=1.2,
multiThreaded=False, # NOT IMPLEMENTED
# Tuning
scalingRatio=scaling_ratio,
strongGravityMode=False,
gravity=gravity,
# Log
verbose=False)
return forceatlas2
def draw(G, labels_dict={}, iteration_force_atlase=2000, figsize=(40, 20), font_size=12, stroke_width=3,
stroke_color="black", font_color="white", edge_cmap=plt.cm.viridis, weight = True):
"""
Return a figure of the current graph
Parameters
----------
G
labels_dict
iteration_force_atlase
figsize
font_size
stroke_width
stroke_color
font_color
edge_cmap
Returns
-------
AxesSubplot
matplotlib canvas
"""
plt.gcf() # Clean previous figure associated with the 'plt' instance
# Compute node position using the Force Atlas algorithm
force_atlas = get_force_atlas()
positions = force_atlas.forceatlas2_networkx_layout(G,
pos=None,
iterations=iteration_force_atlase)
# Initialise the figure canvas
fig, ax = plt.subplots(1, figsize=figsize)
# Draw nodes
nx.draw_networkx_nodes(G, positions, node_color='#999', ax=ax)
# Draw edges
if weight:
weights_width = [G[u][v]['weight'] * 200 for u, v in list(G.edges())]
colors = [G[u][v]['weight'] for u, v in list(G.edges())]
edges = nx.draw_networkx_edges(G, positions, edge_color=colors, width=weights_width,
edge_cmap=edge_cmap, ax=ax)
else:
edges = nx.draw_networkx_edges(G, positions, ax=ax,edge_color="#999")
# Plot nodes label
for node, pos in positions.items():
if labels_dict:
text = ax.text(pos[0], pos[1], labels_dict[node], color=font_color,
ha='center', va='center', size=font_size)
else:
text = ax.text(pos[0], pos[1], node, color=font_color,
ha='center', va='center', size=font_size)
text.set_path_effects([path_effects.Stroke(linewidth=stroke_width, foreground=stroke_color),
path_effects.Normal()]) # effet de style
# Plot colorbar
if weight:
plt.colorbar(edges)
plt.axis("off")