Repository navigation
Expand file tree
/
Copy pathnetwork-graphs.py
More file actions
107 lines (95 loc) · 2.83 KB
/
Copy pathnetwork-graphs.py
File metadata and controls
107 lines (95 loc) · 2.83 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import dash
from dash import dcc, html
import plotly.graph_objects as go
import networkx as nx
import random
# Créer le graphe NetworkX
G = nx.random_geometric_graph(200, 0.125)
# Créer les coordonnées des arêtes
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = G.nodes[edge[0]]['pos']
x1, y1 = G.nodes[edge[1]]['pos']
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines'
)
# Créer les coordonnées des nœuds
node_x = []
node_y = []
for node in G.nodes():
x, y = G.nodes[node]['pos']
node_x.append(x)
node_y.append(y)
# Définir les tailles des nœuds
node_sizes = [random.randint(5, 50) for _ in range(len(node_x))]
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
marker=dict(
showscale=True,
colorscale='YlGnBu',
reversescale=True,
color=[],
size=node_sizes,
colorbar=dict(
thickness=15,
title=dict(
text='Node Connections',
side='right'
),
xanchor='left',
),
line_width=2
)
)
# Ajouter les informations de connectivité
node_adjacencies = []
node_text = []
for node, adjacencies in enumerate(G.adjacency()):
node_adjacencies.append(len(adjacencies[1]))
node_text.append('# of connections: ' + str(len(adjacencies[1])))
node_trace.marker.color = node_adjacencies
node_trace.text = node_text
# Créer la figure Plotly
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title=dict(
text="<br>Network graph made with Python",
font=dict(
size=16
)
),
showlegend=False,
hovermode='closest',
margin=dict(b=20, l=5, r=5, t=40),
annotations=[dict(
text="Python code: <a href='https://plotly.com/python/network-graphs/'> https://plotly.com/python/network-graphs/</a>",
showarrow=False,
xref="paper", yref="paper",
x=0.005, y=-0.002)],
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
)
)
# Créer l'application Dash
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1("Graphique de réseau avec Dash"),
dcc.Graph(
id='network-graph',
figure=fig
)
])
if __name__ == '__main__':
app.run_server(debug=True)