-
Notifications
You must be signed in to change notification settings - Fork 114
/
01.3D_Properly_Colored_Graph_2.py
108 lines (92 loc) · 2.96 KB
/
01.3D_Properly_Colored_Graph_2.py
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
108
import networkx as nx
import matplotlib.pyplot as plt
import plotly.graph_objects as go
# Create an empty graph
G = nx.Graph()
# Add edges to the graph
edges = [(1, 2), (1, 3), (2, 3), (3, 4), (4, 5), (5, 6), (6, 3)]
G.add_edges_from(edges)
# Get the proper coloring of the graph
colors = nx.coloring.greedy_color(G, strategy="largest_first")
# Number of colors used
num_colors = len(set(colors.values()))
# Create a list of color names for visualization
color_names = ['red', 'blue', 'green', 'yellow', 'orange', 'purple', 'cyan', 'black']
# Assign colors to nodes
node_colors = [color_names[colors[node]] for node in G.nodes()]
# Get positions of nodes using spring layout
pos = nx.spring_layout(G, seed=42)
# Calculate the degrees of vertices
degrees = dict(G.degree())
node_size = [v * 100 for v in degrees.values()]
# Create edges trace
edge_trace = go.Scatter(
x=[pos[src][0] for src, dst in G.edges()] + [None],
y=[pos[src][1] for src, dst in G.edges()] + [None],
mode='lines',
line=dict(width=0.5, color='grey'),
hoverinfo='none'
)
# Create nodes trace
node_trace = go.Scatter(
x=[pos[node][0] for node in G.nodes()],
y=[pos[node][1] for node in G.nodes()],
mode='markers',
marker=dict(
showscale=True,
colorscale='Viridis',
size=node_size,
colorbar=dict(
thickness=15,
title='Node Degree',
xanchor='left',
titleside='right'
)
),
text=[f'Degree: {degrees[node]}' for node in G.nodes()],
hoverinfo='text'
)
# Set node colors in the nodes trace
node_trace.marker.color = node_colors
# Create the figure
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='Properly Colored Graph',
titlefont_size=20,
showlegend=False,
hovermode='closest',
margin=dict(b=0, l=0, r=0, t=40),
annotations=[dict(
text="",
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))
)
# Add labels for vertices
for node in G.nodes():
x, y = pos[node]
fig.add_annotation(
x=x, y=y,
text=f'Node {node}<br>Color: {node_colors[node - 1]}<br>Degree: {degrees[node]}',
showarrow=True,
arrowhead=1
)
# Draw legend for colors
for i in range(num_colors):
fig.add_trace(go.Scatter(
x=[None],
y=[None],
mode='markers',
marker=dict(
showscale=False,
size=10,
color=color_names[i]
),
showlegend=False
))
# Show the legend
fig.update_layout(legend_title_text='Colors', legend_traceorder='reversed')
# Show the plot
fig.show()