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 (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 (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

Plotly figures can be exported as:

  • HTML: fig.write_html('plot.html') β€” interactive, self-contained
  • Static: fig.write_image('plot.pdf') β€” requires kaleido
  • 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 β†’\to Matplotlib (vector PDF, full LaTeX support)
  • Presentation β†’\to Either (Plotly for live demos, Matplotlib for slides)
  • Dashboard / web app β†’\to Plotly (native HTML, Dash integration)
  • Exploration β†’\to 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.

Example: Interactive 3D Surface Plot

Plot capacity as a function of SNR and number of antennas using Plotly's 3D surface.

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 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).