Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
12 |
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
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 |
|
17 |
# Helper function to draw and display graph
|
18 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -20,6 +20,68 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
20 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
21 |
st.pyplot(plt)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Function to display Drawing: Labels And Colors
|
24 |
def display_labels_and_colors():
|
25 |
st.title("Drawing: Labels And Colors")
|
|
|
12 |
"Basic: Simple graph Directed", "Drawing: Custom Node Position",
|
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", "Drawing: Multipartite Layout"])
|
16 |
|
17 |
# Helper function to draw and display graph
|
18 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
20 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
21 |
st.pyplot(plt)
|
22 |
|
23 |
+
# Function for creating and drawing a multipartite graph
|
24 |
+
def multilayered_graph(*subset_sizes):
|
25 |
+
extents = nx.utils.pairwise(itertools.accumulate((0,) + subset_sizes))
|
26 |
+
layers = [range(start, end) for start, end in extents]
|
27 |
+
G = nx.Graph()
|
28 |
+
for i, layer in enumerate(layers):
|
29 |
+
G.add_nodes_from(layer, layer=i)
|
30 |
+
for layer1, layer2 in nx.utils.pairwise(layers):
|
31 |
+
G.add_edges_from(itertools.product(layer1, layer2))
|
32 |
+
return G
|
33 |
+
|
34 |
+
# Function to display Drawing: Multipartite Layout
|
35 |
+
def display_multipartite_layout():
|
36 |
+
st.title("Drawing: Multipartite Layout")
|
37 |
+
|
38 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
39 |
+
|
40 |
+
if option == "Default Example":
|
41 |
+
subset_sizes = [5, 5, 4, 3, 2, 4, 4, 3]
|
42 |
+
subset_color = [
|
43 |
+
"gold", "violet", "violet", "violet", "violet",
|
44 |
+
"limegreen", "limegreen", "darkorange"
|
45 |
+
]
|
46 |
+
|
47 |
+
# Generate and plot multipartite graph
|
48 |
+
G = multilayered_graph(*subset_sizes)
|
49 |
+
color = [subset_color[data["layer"]] for v, data in G.nodes(data=True)]
|
50 |
+
pos = nx.multipartite_layout(G, subset_key="layer")
|
51 |
+
|
52 |
+
plt.figure(figsize=(8, 8))
|
53 |
+
nx.draw(G, pos, node_color=color, with_labels=False)
|
54 |
+
plt.axis("equal")
|
55 |
+
st.pyplot(plt)
|
56 |
+
|
57 |
+
elif option == "Create your own":
|
58 |
+
# Let the user input the subset sizes and colors
|
59 |
+
st.write("Enter the subset sizes and colors to create your own multipartite graph.")
|
60 |
+
|
61 |
+
subset_sizes_input = st.text_area("Enter subset sizes (comma-separated, e.g., 5,5,4,3):", value="5,5,4,3,2,4,4,3")
|
62 |
+
subset_sizes = list(map(int, subset_sizes_input.split(',')))
|
63 |
+
|
64 |
+
subset_colors_input = st.text_area("Enter subset colors (comma-separated, e.g., gold,violet,green):", value="gold,violet,violet,violet,violet,limegreen,limegreen,darkorange")
|
65 |
+
subset_colors = subset_colors_input.split(',')
|
66 |
+
|
67 |
+
# Validate input
|
68 |
+
if len(subset_sizes) == len(subset_colors):
|
69 |
+
# Generate and plot multipartite graph
|
70 |
+
G = multilayered_graph(*subset_sizes)
|
71 |
+
color = [subset_colors[data["layer"]] for v, data in G.nodes(data=True)]
|
72 |
+
pos = nx.multipartite_layout(G, subset_key="layer")
|
73 |
+
|
74 |
+
plt.figure(figsize=(8, 8))
|
75 |
+
nx.draw(G, pos, node_color=color, with_labels=False)
|
76 |
+
plt.axis("equal")
|
77 |
+
st.pyplot(plt)
|
78 |
+
else:
|
79 |
+
st.error("The number of colors should match the number of subsets.")
|
80 |
+
|
81 |
+
# Display Drawing: Multipartite Layout if selected
|
82 |
+
if sidebar_option == "Drawing: Multipartite Layout":
|
83 |
+
display_multipartite_layout()
|
84 |
+
|
85 |
# Function to display Drawing: Labels And Colors
|
86 |
def display_labels_and_colors():
|
87 |
st.title("Drawing: Labels And Colors")
|