Create an app showing 3D data visualizations. Generate mock 3D data: - Spiral helix with noise (100 points) - Surface plot (z = sin(x)*cos(y)) - 3D clusters (3 clusters, 50 points each) Features: - Viz selector dropdown - Interactive 3D plots with rotate/zoom - Controls for: * Point styling (size, color, opacity) * Axis settings * Animation speed Surface plot options: - Function parameter sliders - Color scheme toggle - Contour lines All plots should be fully interactive.
To upload files, please first save the app
import streamlit as st
import numpy as np
import plotly.graph_objects as go
from sklearn.datasets import make_blobs
# Set page config
st.set_page_config(page_title="3D Visualization Explorer", layout="wide")
# Utility functions
def generate_spiral():
t = np.linspace(0, 10*np.pi, 100)
x = t * np.cos(t)
y = t * np.sin(t)
z = t + np.random.normal(0, 0.5, 100)
return x, y, z
def generate_surface(a=1, b=1):
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = a * np.sin(X) * np.cos(b * Y)
return X, Y, Z
def generate_clusters():
X, y = make_blobs(n_samples=150, n_features=3, centers=3, cluster_std=1.0, random_state=42)
return X[:, 0], X[:, 1], X[:, 2], y
# Main app
st.title("3D Data Visualization Explorer")
# Sidebar controls
st.sidebar.header("Visualization Controls")
viz_type = st.sidebar.selectbox(
"Select Visualization",
["Spiral Helix", "Surface Plot", "3D Clusters"]
)
# Common plot settings
point_size = st.sidebar.slider("Point Size", 1, 20, 5)
opacity = st.sidebar.slider("Opacity", 0.0, 1.0, 0.8)
colorscale = st.sidebar.selectbox("Color Scheme", ["Viridis", "Plasma", "Magma", "Inferno"])
# Create figure based on selection
fig = go.Figure()
if viz_type == "Spiral Helix":
x, y, z = generate_spiral()
animation_speed = st.sidebar.slider("Animation Speed", 0, 100, 50)
fig.add_trace(go.Scatter3d(
x=x, y=y, z=z,
mode='markers',
marker=dict(
size=point_size,
opacity=opacity,
colorscale=colorscale.lower(),
color=z,
)
))
elif viz_type == "Surface Plot":
st.sidebar.subheader("Surface Plot Controls")
a = st.sidebar.slider("Sine multiplier", 0.1, 5.0, 1.0)
b = st.sidebar.slider("Cosine multiplier", 0.1, 5.0, 1.0)
show_contours = st.sidebar.checkbox("Show Contours", True)
X, Y, Z = generate_surface(a, b)
fig.add_trace(go.Surface(
x=X, y=Y, z=Z,
colorscale=colorscale.lower(),
opacity=opacity,
contours=dict(
z=dict(
show=show_contours,
usecolormap=True,
project_z=True
)
)
))
else: # 3D Clusters
x, y, z, labels = generate_clusters()
fig.add_trace(go.Scatter3d(
x=x, y=y, z=z,
mode='markers',
marker=dict(
size=point_size,
opacity=opacity,
color=labels,
colorscale=colorscale.lower(),
)
))
# Common figure settings
fig.update_layout(
scene=dict(
xaxis_title="X",
yaxis_title="Y",
zaxis_title="Z",
aspectmode='cube'
),
width=800,
height=800,
margin=dict(l=0, r=0, b=0, t=30)
)
# Display plot
st.plotly_chart(fig, use_container_width=True)
# Info section
with st.expander("About this visualization"):
if viz_type == "Spiral Helix":
st.write("""
This visualization shows a 3D spiral helix with added random noise.
The color of each point corresponds to its z-coordinate.
""")
elif viz_type == "Surface Plot":
st.write("""
This visualization shows a surface plot of the function z = sin(ax)cos(by).
Use the sliders to adjust the parameters a and b to see how they affect the surface.
""")
else:
st.write("""
This visualization shows three 3D clusters generated using scikit-learn's make_blobs function.
Each color represents a different cluster.
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?