Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,8 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
13 |
"Drawing: Cluster Layout", "Drawing: Degree Analysis",
|
14 |
"Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
|
15 |
"Drawing: House With Colors", "Drawing: Labels And Colors",
|
16 |
-
"Drawing: Multipartite Layout", "Drawing: Node Colormap"
|
|
|
17 |
|
18 |
|
19 |
# Helper function to draw and display graph
|
@@ -22,6 +23,105 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
22 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
23 |
st.pyplot(plt)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Function to display Drawing: Node Colormap
|
26 |
def display_node_colormap():
|
27 |
st.title("Drawing: Node Colormap")
|
|
|
13 |
"Drawing: Cluster Layout", "Drawing: Degree Analysis",
|
14 |
"Drawing: Ego Graph", "Drawing: Eigenvalues", "Drawing: Four Grids",
|
15 |
"Drawing: House With Colors", "Drawing: Labels And Colors",
|
16 |
+
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
17 |
+
"Drawing: Rainbow Coloring"])
|
18 |
|
19 |
|
20 |
# Helper function to draw and display graph
|
|
|
23 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
24 |
st.pyplot(plt)
|
25 |
|
26 |
+
# Function to display Drawing: Rainbow Coloring
|
27 |
+
def display_rainbow_coloring():
|
28 |
+
st.title("Drawing: Rainbow Coloring")
|
29 |
+
|
30 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
31 |
+
|
32 |
+
if option == "Default Example":
|
33 |
+
# Rainbow Coloring with default parameters
|
34 |
+
node_dist_to_color = {
|
35 |
+
1: "tab:red",
|
36 |
+
2: "tab:orange",
|
37 |
+
3: "tab:olive",
|
38 |
+
4: "tab:green",
|
39 |
+
5: "tab:blue",
|
40 |
+
6: "tab:purple",
|
41 |
+
}
|
42 |
+
|
43 |
+
nnodes = 13
|
44 |
+
G = nx.complete_graph(nnodes)
|
45 |
+
|
46 |
+
n = (nnodes - 1) // 2
|
47 |
+
ndist_iter = list(range(1, n + 1))
|
48 |
+
ndist_iter += ndist_iter[::-1]
|
49 |
+
|
50 |
+
def cycle(nlist, n):
|
51 |
+
return nlist[-n:] + nlist[:-n]
|
52 |
+
|
53 |
+
nodes = list(G.nodes())
|
54 |
+
for i, nd in enumerate(ndist_iter):
|
55 |
+
for u, v in zip(nodes, cycle(nodes, i + 1)):
|
56 |
+
G[u][v]["color"] = node_dist_to_color[nd]
|
57 |
+
|
58 |
+
pos = nx.circular_layout(G)
|
59 |
+
# Create a figure with 1:1 aspect ratio to preserve the circle.
|
60 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
61 |
+
node_opts = {"node_size": 500, "node_color": "w", "edgecolors": "k", "linewidths": 2.0}
|
62 |
+
nx.draw_networkx_nodes(G, pos, **node_opts)
|
63 |
+
nx.draw_networkx_labels(G, pos, font_size=14)
|
64 |
+
# Extract color from edge data
|
65 |
+
edge_colors = [edgedata["color"] for _, _, edgedata in G.edges(data=True)]
|
66 |
+
nx.draw_networkx_edges(G, pos, width=2.0, edge_color=edge_colors)
|
67 |
+
|
68 |
+
ax.set_axis_off()
|
69 |
+
fig.tight_layout()
|
70 |
+
st.pyplot(plt)
|
71 |
+
|
72 |
+
elif option == "Create your own":
|
73 |
+
nnodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=13)
|
74 |
+
|
75 |
+
# Allow users to create their own color map
|
76 |
+
red = st.color_picker("Select a color for Red (1)", "#ff0000")
|
77 |
+
orange = st.color_picker("Select a color for Orange (2)", "#ff7f00")
|
78 |
+
olive = st.color_picker("Select a color for Olive (3)", "#808000")
|
79 |
+
green = st.color_picker("Select a color for Green (4)", "#008000")
|
80 |
+
blue = st.color_picker("Select a color for Blue (5)", "#0000ff")
|
81 |
+
purple = st.color_picker("Select a color for Purple (6)", "#800080")
|
82 |
+
|
83 |
+
node_dist_to_color = {
|
84 |
+
1: red,
|
85 |
+
2: orange,
|
86 |
+
3: olive,
|
87 |
+
4: green,
|
88 |
+
5: blue,
|
89 |
+
6: purple,
|
90 |
+
}
|
91 |
+
|
92 |
+
if st.button("Generate Graph"):
|
93 |
+
G = nx.complete_graph(nnodes)
|
94 |
+
|
95 |
+
n = (nnodes - 1) // 2
|
96 |
+
ndist_iter = list(range(1, n + 1))
|
97 |
+
ndist_iter += ndist_iter[::-1]
|
98 |
+
|
99 |
+
def cycle(nlist, n):
|
100 |
+
return nlist[-n:] + nlist[:-n]
|
101 |
+
|
102 |
+
nodes = list(G.nodes())
|
103 |
+
for i, nd in enumerate(ndist_iter):
|
104 |
+
for u, v in zip(nodes, cycle(nodes, i + 1)):
|
105 |
+
G[u][v]["color"] = node_dist_to_color[nd]
|
106 |
+
|
107 |
+
pos = nx.circular_layout(G)
|
108 |
+
# Create a figure with 1:1 aspect ratio to preserve the circle.
|
109 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
110 |
+
node_opts = {"node_size": 500, "node_color": "w", "edgecolors": "k", "linewidths": 2.0}
|
111 |
+
nx.draw_networkx_nodes(G, pos, **node_opts)
|
112 |
+
nx.draw_networkx_labels(G, pos, font_size=14)
|
113 |
+
# Extract color from edge data
|
114 |
+
edge_colors = [edgedata["color"] for _, _, edgedata in G.edges(data=True)]
|
115 |
+
nx.draw_networkx_edges(G, pos, width=2.0, edge_color=edge_colors)
|
116 |
+
|
117 |
+
ax.set_axis_off()
|
118 |
+
fig.tight_layout()
|
119 |
+
st.pyplot(plt)
|
120 |
+
|
121 |
+
# Display Drawing: Rainbow Coloring if selected
|
122 |
+
if sidebar_option == "Drawing: Rainbow Coloring":
|
123 |
+
display_rainbow_coloring()
|
124 |
+
|
125 |
# Function to display Drawing: Node Colormap
|
126 |
def display_node_colormap():
|
127 |
st.title("Drawing: Node Colormap")
|