create an app that allows me to visualize 3d data
To upload files, please first save the app
import streamlit as st
import numpy as np
import plotly.graph_objects as go
st.title("3D Data Visualization")
# Sidebar controls
st.sidebar.header("Plot Controls")
plot_type = st.sidebar.selectbox(
"Select Plot Type",
["Surface Plot", "Scatter 3D", "Line 3D"]
)
# Generate sample data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
# Different functions for visualization
def surface_plot():
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = go.Figure(data=[go.Surface(x=x, y=y, z=Z)])
fig.update_layout(
title='Surface Plot',
scene = dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
),
width=700,
height=700
)
st.plotly_chart(fig)
def scatter_3d():
# Generate random 3D points
num_points = 1000
x = np.random.normal(0, 1, num_points)
y = np.random.normal(0, 1, num_points)
z = np.random.normal(0, 1, num_points)
fig = go.Figure(data=[go.Scatter3d(
x=x, y=y, z=z,
mode='markers',
marker=dict(
size=5,
color=z,
colorscale='Viridis',
opacity=0.8
)
)])
fig.update_layout(
title='3D Scatter Plot',
scene = dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
),
width=700,
height=700
)
st.plotly_chart(fig)
def line_3d():
t = np.linspace(0, 10, 100)
x = np.sin(t)
y = np.cos(t)
z = t
fig = go.Figure(data=[go.Scatter3d(
x=x, y=y, z=z,
mode='lines',
line=dict(
color=t,
width=5,
colorscale='Viridis'
)
)])
fig.update_layout(
title='3D Line Plot',
scene = dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
),
width=700,
height=700
)
st.plotly_chart(fig)
# Display the selected plot
if plot_type == "Surface Plot":
surface_plot()
elif plot_type == "Scatter 3D":
scatter_3d()
else:
line_3d()
# Add explanation
st.sidebar.markdown("""
## About this app
This app demonstrates different types of 3D visualizations:
- **Surface Plot**: Shows a 3D surface defined by z = f(x,y)
- **Scatter 3D**: Displays random points in 3D space
- **Line 3D**: Shows a parametric curve in 3D space
Use the dropdown menu above to switch between different visualization types.
You can rotate, zoom, and pan the plots using your mouse!
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?