Interactive Plots with Plotly
When Static Plots Are Not Enough
Static plots are perfect for papers, but interactive plots are
essential for data exploration. Plotly provides zoom, pan, hover
tooltips, and linked brushing out of the box. Its plotly.express
API mirrors Seaborn's simplicity, while plotly.graph_objects gives
full control.
Definition: Plotly Express β One-Line Interactive Plots
Plotly Express β One-Line Interactive Plots
plotly.express (commonly px) wraps plotly.graph_objects with a
tidy-data interface:
import plotly.express as px
fig = px.scatter(df, x='snr_db', y='throughput', color='modulation',
size='n_antennas', hover_data=['ber'],
title='Throughput vs. SNR')
fig.show()
Key arguments: color, symbol, size, facet_col, facet_row,
animation_frame, hover_data, trendline.
Definition: Plotly Graph Objects β Full Control
Plotly Graph Objects β Full Control
plotly.graph_objects (go) provides fine-grained control:
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=t, y=signal, mode='lines',
name='Signal'), row=1, col=1)
fig.add_trace(go.Heatmap(z=spectrogram, colorscale='Viridis'),
row=1, col=2)
fig.update_layout(title='Signal Analysis')
Use px for quick exploration and go for dashboards and production
visualizations.
Definition: Exporting Plotly Figures
Exporting Plotly Figures
Plotly figures can be exported as:
- HTML:
fig.write_html('plot.html')β interactive, self-contained - Static:
fig.write_image('plot.pdf')β requireskaleido - JSON:
fig.to_json()β for web embedding
# Install: pip install kaleido
fig.write_image('plot.pdf', width=800, height=500, scale=2)
Theorem: When to Use Plotly vs. Matplotlib
Choose your plotting library based on the output medium:
- Journal paper Matplotlib (vector PDF, full LaTeX support)
- Presentation Either (Plotly for live demos, Matplotlib for slides)
- Dashboard / web app Plotly (native HTML, Dash integration)
- Exploration Plotly (hover, zoom, pan for free)
The two are not mutually exclusive: explore with Plotly, then reproduce final figures in Matplotlib for publication.
Plotly excels at interactivity; Matplotlib excels at typographic control. Most workflows use both.
Example: Interactive SNR-Throughput Explorer
Create an interactive scatter plot where hovering reveals BER, and color encodes modulation type.
Implementation
import plotly.express as px
import pandas as pd
import numpy as np
rng = np.random.default_rng(42)
df = pd.DataFrame({
'snr_db': rng.uniform(0, 30, 200),
'modulation': rng.choice(['BPSK','QPSK','16-QAM'], 200),
})
df['throughput'] = 10 * np.log2(1 + 10**(df['snr_db']/10))
df['throughput'] += rng.normal(0, 2, 200)
df['ber'] = 10**(-df['snr_db']/10 + rng.normal(0, 0.3, 200))
fig = px.scatter(df, x='snr_db', y='throughput',
color='modulation',
hover_data=['ber'],
title='Throughput vs SNR')
fig.show()
Example: Interactive 3D Surface Plot
Plot capacity as a function of SNR and number of antennas using Plotly's 3D surface.
Implementation
import plotly.graph_objects as go
import numpy as np
snr_db = np.linspace(0, 30, 50)
n_ant = np.arange(1, 9)
SNR, N = np.meshgrid(snr_db, n_ant)
snr_lin = 10**(SNR / 10)
capacity = N * np.log2(1 + snr_lin / N)
fig = go.Figure(data=[go.Surface(
x=snr_db, y=n_ant, z=capacity,
colorscale='Viridis'
)])
fig.update_layout(
title='MIMO Capacity Surface',
scene=dict(xaxis_title='SNR (dB)',
yaxis_title='Antennas',
zaxis_title='Capacity (bits/s/Hz)'),
)
fig.show()
Interactive Plotly Scatter
Explore the relationship between SNR, throughput, and BER with interactive hover tooltips and color encoding.
Parameters
Common Mistake: Plotly HTML Files Can Be Huge
Mistake:
Embedding 100,000+ data points in a Plotly HTML file creates multi-megabyte files that load slowly in browsers.
Correction:
For large datasets, downsample for visualization or use
datashader for server-side rendering. For static exports,
use fig.write_image() with kaleido.
Quick Check
When is Plotly preferred over Matplotlib?
When you need LaTeX labels in a journal figure
When you need interactive zoom, pan, and hover in a browser
When you need the smallest possible file size
When you need to print at 600 DPI
Plotly provides native HTML interactivity without any extra code.
Plotly Express
A high-level Plotly API that creates interactive plots from DataFrames in one function call, similar to Seaborn's interface.
Dash
A Python web framework by Plotly for building interactive analytical dashboards with pure Python (no JavaScript required).