Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
| 20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
| 21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
| 22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
| 23 |
-
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation"])
|
| 24 |
|
| 25 |
# Helper function to draw and display graph
|
| 26 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
@@ -92,7 +92,96 @@ if sidebar_option == "3D Drawing: Animations of 3D Rotation":
|
|
| 92 |
ani.save("animation.gif", writer="imagemagick")
|
| 93 |
st.image("animation.gif", caption="3D Graph Rotation", use_container_width=True)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Function to display Weighted Graph
|
| 98 |
def display_weighted_graph():
|
|
|
|
| 20 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
| 21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
| 22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
| 23 |
+
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib"])
|
| 24 |
|
| 25 |
# Helper function to draw and display graph
|
| 26 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
|
| 92 |
ani.save("animation.gif", writer="imagemagick")
|
| 93 |
st.image("animation.gif", caption="3D Graph Rotation", use_container_width=True)
|
| 94 |
|
| 95 |
+
# Default example code
|
| 96 |
+
def default_example():
|
| 97 |
+
G = nx.cycle_graph(20)
|
| 98 |
+
|
| 99 |
+
# 3d spring layout
|
| 100 |
+
pos = nx.spring_layout(G, dim=3, seed=779)
|
| 101 |
+
# Extract node and edge positions from the layout
|
| 102 |
+
node_xyz = np.array([pos[v] for v in sorted(G)])
|
| 103 |
+
edge_xyz = np.array([(pos[u], pos[v]) for u, v in G.edges()])
|
| 104 |
+
|
| 105 |
+
# Create the 3D figure
|
| 106 |
+
fig = plt.figure()
|
| 107 |
+
ax = fig.add_subplot(111, projection="3d")
|
| 108 |
+
|
| 109 |
+
# Plot the nodes - alpha is scaled by "depth" automatically
|
| 110 |
+
ax.scatter(*node_xyz.T, s=100, ec="w")
|
| 111 |
+
|
| 112 |
+
# Plot the edges
|
| 113 |
+
for vizedge in edge_xyz:
|
| 114 |
+
ax.plot(*vizedge.T, color="tab:gray")
|
| 115 |
+
|
| 116 |
+
def _format_axes(ax):
|
| 117 |
+
"""Visualization options for the 3D axes."""
|
| 118 |
+
# Turn gridlines off
|
| 119 |
+
ax.grid(False)
|
| 120 |
+
# Suppress tick labels
|
| 121 |
+
for dim in (ax.xaxis, ax.yaxis, ax.zaxis):
|
| 122 |
+
dim.set_ticks([])
|
| 123 |
+
# Set axes labels
|
| 124 |
+
ax.set_xlabel("x")
|
| 125 |
+
ax.set_ylabel("y")
|
| 126 |
+
ax.set_zlabel("z")
|
| 127 |
+
|
| 128 |
+
_format_axes(ax)
|
| 129 |
+
fig.tight_layout()
|
| 130 |
+
st.pyplot(fig)
|
| 131 |
+
|
| 132 |
+
# Create your own graph option
|
| 133 |
+
def create_own_graph():
|
| 134 |
+
# Input fields to customize the graph
|
| 135 |
+
nodes = st.number_input("Number of nodes", min_value=2, max_value=50, value=20)
|
| 136 |
+
seed = st.number_input("Seed for layout", value=779)
|
| 137 |
+
|
| 138 |
+
# Generate graph and layout
|
| 139 |
+
G = nx.cycle_graph(nodes)
|
| 140 |
+
pos = nx.spring_layout(G, dim=3, seed=seed)
|
| 141 |
+
|
| 142 |
+
# Extract node and edge positions
|
| 143 |
+
node_xyz = np.array([pos[v] for v in sorted(G)])
|
| 144 |
+
edge_xyz = np.array([(pos[u], pos[v]) for u, v in G.edges()])
|
| 145 |
+
|
| 146 |
+
# Create the 3D figure
|
| 147 |
+
fig = plt.figure()
|
| 148 |
+
ax = fig.add_subplot(111, projection="3d")
|
| 149 |
+
|
| 150 |
+
# Plot the nodes
|
| 151 |
+
ax.scatter(*node_xyz.T, s=100, ec="w")
|
| 152 |
+
|
| 153 |
+
# Plot the edges
|
| 154 |
+
for vizedge in edge_xyz:
|
| 155 |
+
ax.plot(*vizedge.T, color="tab:gray")
|
| 156 |
+
|
| 157 |
+
def _format_axes(ax):
|
| 158 |
+
"""Visualization options for the 3D axes."""
|
| 159 |
+
ax.grid(False)
|
| 160 |
+
for dim in (ax.xaxis, ax.yaxis, ax.zaxis):
|
| 161 |
+
dim.set_ticks([])
|
| 162 |
+
ax.set_xlabel("x")
|
| 163 |
+
ax.set_ylabel("y")
|
| 164 |
+
ax.set_zlabel("z")
|
| 165 |
+
|
| 166 |
+
_format_axes(ax)
|
| 167 |
+
fig.tight_layout()
|
| 168 |
+
st.pyplot(fig)
|
| 169 |
+
|
| 170 |
+
if sidebar_option == "3D Drawing: Basic Matplotlib":
|
| 171 |
+
st.title("3D Drawing: Basic Matplotlib")
|
| 172 |
|
| 173 |
+
# Provide options for Default Example or Custom Graph
|
| 174 |
+
graph_mode = st.radio(
|
| 175 |
+
"Choose a Mode:",
|
| 176 |
+
("Default Example", "Create Your Own"),
|
| 177 |
+
help="Default example shows a dodecahedral graph, or you can create your own custom graph."
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Display the chosen option
|
| 181 |
+
if graph_mode == "Default Example":
|
| 182 |
+
default_example()
|
| 183 |
+
elif graph_mode == "Create Your Own":
|
| 184 |
+
create_own_graph()
|
| 185 |
|
| 186 |
# Function to display Weighted Graph
|
| 187 |
def display_weighted_graph():
|