Interactive Data Visualization with Plotly
Introduction to Plotly
Plotly is a powerful library for creating interactive visualizations in Python, perfect for web-based scientific presentations.
Basic Interactive Plot
import plotly.graph_objects as go
import numpy as np
# Generate data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create interactive plot
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='sin(x)'))
# Update layout
fig.update_layout(
title='Interactive Sine Wave',
xaxis_title='x',
yaxis_title='sin(x)'
)
# Show plot
fig.show()
3D Visualization
Create interactive 3D plots for complex physical systems.
3D Surface Plot
import plotly.graph_objects as go
import numpy as np
# Create 3D data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
# Create 3D surface plot
fig = go.Figure(data=[go.Surface(z=Z, x=x, y=y)])
# Update layout
fig.update_layout(
title='3D Wave Function',
scene = dict(
xaxis_title='X',
yaxis_title='Y',
zaxis_title='Z'
)
)
fig.show()
Interactive Dashboards
Learn how to create interactive dashboards for data analysis.
Simple Dashboard
import plotly.express as px
import pandas as pd
import numpy as np
# Create sample data
np.random.seed(42)
data = {
'Time': np.linspace(0, 10, 100),
'Temperature': 25 + np.random.normal(0, 1, 100),
'Pressure': 1 + 0.1 * np.sin(np.linspace(0, 10, 100))
}
df = pd.DataFrame(data)
# Create interactive line plot
fig = px.line(df, x='Time', y=['Temperature', 'Pressure'],
title='Temperature and Pressure vs Time')
# Update layout
fig.update_layout(
xaxis_title='Time (s)',
yaxis_title='Values',
hovermode='x unified'
)
fig.show()
Practice Exercises
Exercise 1: Quantum States
Create an interactive 3D visualization of quantum harmonic oscillator wavefunctions.
Exercise 2: Data Analysis
Build an interactive dashboard to analyze and visualize experimental data from a physics experiment.