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 plotly.graph_objects as go
import numpy as np
from sklearn.datasets import make_blobs
import plotly.express as px
# Page config
st.set_page_config(layout="wide")
st.title("3D Data Visualization Explorer")
# Sidebar controls
viz_type = st.sidebar.selectbox(
"Select Visualization",
["Spiral Helix", "Surface Plot", "3D Clusters"]
)
# Common plot settings
st.sidebar.subheader("Plot Settings")
marker_size = st.sidebar.slider("Marker Size", 2, 10, 5)
opacity = st.sidebar.slider("Opacity", 0.0, 1.0, 0.8)
colorscale = st.sidebar.selectbox(
"Color Scheme",
["Viridis", "Plasma", "Inferno", "Magma", "Blues", "Reds"]
)
def generate_helix(n_points=100):
t = np.linspace(0, 10*np.pi, n_points)
noise = np.random.normal(0, 0.1, n_points)
x = np.cos(t) + noise
y = np.sin(t) + noise
z = t/10 + noise
return x, y, z
def generate_surface():
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)
return X, Y, Z
def generate_clusters():
X, y = make_blobs(n_samples=150, n_features=3, centers=3, random_state=42)
return X[:, 0], X[:, 1], X[:, 2], y
if viz_type == "Spiral Helix":
# Animation speed control
speed = st.sidebar.slider("Animation Speed", 0, 100, 50)
x, y, z = generate_helix()
fig = go.Figure(data=[go.Scatter3d(
x=x, y=y, z=z,
mode='markers',
marker=dict(
size=marker_size,
opacity=opacity,
color=z,
colorscale=colorscale
)
)])
# Add animation
if speed > 0:
frames = []
for i in range(len(x)):
frames.append(go.Frame(data=[go.Scatter3d(
x=x[:i+1], y=y[:i+1], z=z[:i+1],
mode='markers',
marker=dict(
size=marker_size,
opacity=opacity,
color=z[:i+1],
colorscale=colorscale
)
)]))
fig.frames = frames
fig.update_layout(
updatemenus=[dict(
type="buttons",
showactive=False,
buttons=[dict(
label="Play",
method="animate",
args=[None, dict(frame=dict(duration=101-speed, redraw=True), fromcurrent=True)]
)]
)]
)
elif viz_type == "Surface Plot":
# Surface plot specific controls
st.sidebar.subheader("Surface Settings")
freq_x = st.sidebar.slider("X Frequency", 0.1, 2.0, 1.0)
freq_y = st.sidebar.slider("Y Frequency", 0.1, 2.0, 1.0)
show_contours = st.sidebar.checkbox("Show Contours", True)
X, Y, Z = generate_surface()
Z = np.sin(freq_x * X) * np.cos(freq_y * Y)
fig = go.Figure(data=[go.Surface(
x=X, y=Y, z=Z,
opacity=opacity,
colorscale=colorscale,
contours=dict(
x=dict(show=show_contours, highlight=True),
y=dict(show=show_contours, highlight=True),
z=dict(show=show_contours, highlight=True)
)
)])
else: # 3D Clusters
x, y, z, labels = generate_clusters()
fig = go.Figure(data=[go.Scatter3d(
x=x, y=y, z=z,
mode='markers',
marker=dict(
size=marker_size,
opacity=opacity,
color=labels,
colorscale=colorscale
)
)])
# Common layout 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)
# Add plot description
if viz_type == "Spiral Helix":
st.markdown("""
**Spiral Helix Visualization**
- 100 points forming a 3D helix with added Gaussian noise
- Color intensity based on z-coordinate
- Animated drawing option with adjustable speed
""")
elif viz_type == "Surface Plot":
st.markdown("""
**Surface Plot Visualization**
- Interactive 3D surface of z = sin(fx*x)*cos(fy*y)
- Adjustable x and y frequencies
- Optional contour lines on all axes
""")
else:
st.markdown("""
**3D Clusters Visualization**
- 3 clusters with 50 points each
- Generated using scikit-learn's make_blobs
- Color indicates cluster membership
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?