Update app.py
Browse files
app.py
CHANGED
|
@@ -119,7 +119,7 @@ def default_example():
|
|
| 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")
|
|
@@ -135,37 +135,41 @@ def create_own_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 |
-
#
|
| 139 |
-
|
| 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 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
ax.plot(*vizedge.T, color="tab:gray")
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 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 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
if sidebar_option == "3D Drawing: Basic Matplotlib":
|
| 171 |
st.title("3D Drawing: Basic Matplotlib")
|
|
@@ -174,7 +178,7 @@ if sidebar_option == "3D Drawing: Basic Matplotlib":
|
|
| 174 |
graph_mode = st.radio(
|
| 175 |
"Choose a Mode:",
|
| 176 |
("Default Example", "Create Your Own"),
|
| 177 |
-
help="Default example shows a
|
| 178 |
)
|
| 179 |
|
| 180 |
# Display the chosen option
|
|
|
|
| 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")
|
|
|
|
| 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 |
+
# Add a button to generate the graph
|
| 139 |
+
generate_button = st.button("Generate Graph")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
if generate_button:
|
| 142 |
+
# Generate graph and layout
|
| 143 |
+
G = nx.cycle_graph(nodes)
|
| 144 |
+
pos = nx.spring_layout(G, dim=3, seed=seed)
|
| 145 |
+
|
| 146 |
+
# Extract node and edge positions
|
| 147 |
+
node_xyz = np.array([pos[v] for v in sorted(G)])
|
| 148 |
+
edge_xyz = np.array([(pos[u], pos[v]) for u, v in G.edges()])
|
| 149 |
|
| 150 |
+
# Create the 3D figure
|
| 151 |
+
fig = plt.figure()
|
| 152 |
+
ax = fig.add_subplot(111, projection="3d")
|
| 153 |
|
| 154 |
+
# Plot the nodes
|
| 155 |
+
ax.scatter(*node_xyz.T, s=100, ec="w")
|
|
|
|
| 156 |
|
| 157 |
+
# Plot the edges
|
| 158 |
+
for vizedge in edge_xyz:
|
| 159 |
+
ax.plot(*vizedge.T, color="tab:gray")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
def _format_axes(ax):
|
| 162 |
+
"""Visualization options for the 3D axes."""
|
| 163 |
+
ax.grid(False)
|
| 164 |
+
for dim in (ax.xaxis, ax.yaxis, ax.zaxis):
|
| 165 |
+
dim.set_ticks([])
|
| 166 |
+
ax.set_xlabel("x")
|
| 167 |
+
ax.set_ylabel("y")
|
| 168 |
+
ax.set_zlabel("z")
|
| 169 |
+
|
| 170 |
+
_format_axes(ax)
|
| 171 |
+
fig.tight_layout()
|
| 172 |
+
st.pyplot(fig)
|
| 173 |
|
| 174 |
if sidebar_option == "3D Drawing: Basic Matplotlib":
|
| 175 |
st.title("3D Drawing: Basic Matplotlib")
|
|
|
|
| 178 |
graph_mode = st.radio(
|
| 179 |
"Choose a Mode:",
|
| 180 |
("Default Example", "Create Your Own"),
|
| 181 |
+
help="Default example shows a cycle graph, or you can create your own custom graph."
|
| 182 |
)
|
| 183 |
|
| 184 |
# Display the chosen option
|