Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

plot.py 1.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. import matplotlib.pyplot as plt
  2. import networkx as nx
  3. import plotly.offline as py
  4. import plotly.graph_objects as go
  5. import plotly
  6. def plot_network_graph(G, list_of_colors_by_order_of_nodes, list_of_text_by_order_of_nodes, TITLE):
  7. # Nodes
  8. # color nodes by recommendation level
  9. # get the location of the nodes
  10. pos = nx.spring_layout(G)
  11. Xn = [pos[k][0] for k in pos.keys()]
  12. Yn = [pos[k][1] for k in pos.keys()]
  13. trace_nodes = dict(type='scatter',
  14. x=Xn,
  15. y=Yn,
  16. mode='markers',
  17. marker=dict(size=10, color=list_of_colors_by_order_of_nodes),
  18. hoverinfo='text',
  19. text=list_of_text_by_order_of_nodes,
  20. )
  21. # Edges
  22. Xe=[]
  23. Ye=[]
  24. for e in G.edges():
  25. Xe.extend([pos[e[0]][0], pos[e[1]][0], None])
  26. Ye.extend([pos[e[0]][1], pos[e[1]][1], None])
  27. trace_edges=dict(type='scatter',
  28. mode='lines',
  29. x=Xe,
  30. y=Ye,
  31. line=dict(width=1, color='#555555'),
  32. hoverinfo='none'
  33. )
  34. # visualize the network
  35. axis=dict(showline=False, # hide axis line, grid, ticklabels and title
  36. zeroline=False,
  37. showgrid=False,
  38. showticklabels=False,
  39. title=''
  40. )
  41. layout=dict(title=TITLE,
  42. width=600,
  43. height=600,
  44. autosize=False,
  45. showlegend=False,
  46. xaxis=axis,
  47. yaxis=axis,
  48. margin=dict(l=40,r=40,b=85,t=100,pad=0,),
  49. hovermode='closest',
  50. plot_bgcolor='#ffffff', #set background color
  51. )
  52. fig = dict(data=[trace_edges, trace_nodes], layout=layout)
  53. return fig
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...