Skip to main content

Generate responsive HTML image maps from Matplotlib figures

Project description

matplotlib-interactive-html

Generate responsive, self-contained HTML image maps from Matplotlib figures.

Each data point becomes a clickable hotspot that can open a linked file (image, video, HTML page, PDF, etc.) in a new browser tab or in an optional side preview panel.

Requirements: Python 3.9+, Matplotlib 3.5+

Install from PyPI as matplotlib-interactive-html, import as matplotlib_interactive.

Installation

pip install matplotlib-interactive-html

From source (development):

pip install -e .

Use a virtual environment if your system Python is externally managed (PEP 668):

python3 -m venv .venv
source .venv/bin/activate
pip install -e .

Development dependencies:

pip install -e ".[dev]"
playwright install chromium

Quick start

import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt

from matplotlib_interactive import make_plot_interactive

fig, ax = plt.subplots(figsize=(8, 6))
x = [1, 2, 3]
y = [10, 20, 15]
labels = ["point_A.png", "point_B.png", "point_C.png"]

ax.plot(x, y, "o")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.grid(True, alpha=0.3)

html_path = make_plot_interactive(
    fig,
    ax,
    x,
    y,
    labels,
    "output/plot.html",
)

print(f"Saved: {html_path}")

Open the HTML file by double-clicking it in your file manager. No server is required for images.

Place linked files (point_A.png, etc.) in the same folder as the HTML file, or use relative paths from that folder.

Classic mode (new tab)

By default (iframe_preview=None), each hotspot opens its linked file in a new browser tab (target="_blank"). No side panel is added to the page.

Preview panel

Enable an in-page side panel instead of opening files in a new tab:

from matplotlib_interactive import IframePreview, Theme, make_plot_interactive

make_plot_interactive(
    fig,
    ax,
    x,
    y,
    labels,
    "output/plot.html",
    iframe_preview=IframePreview(),
    theme=Theme(mode="auto"),
)

Features:

  • Auto-sized layout: plot and preview panel share the viewport
  • Draggable splitter between plot and panel (drag to resize)
  • SVG hotspot markers: hidden at rest, shown on direct hover; selected point highlighted with accent ring and pulse
  • Automatic media type detection (image, video, HTML, PDF)
  • Panel header with filename, open-in-new-tab button, and close button
  • Ctrl+click (Cmd+click on macOS) on a hotspot opens in a new tab

Resizable splitter

In auto layout mode (IframePreview() without explicit width/height), a vertical handle appears between the plot and the preview panel when a file is opened. Drag it to adjust how much space each area uses.

IframePreview(
    initial_plot_ratio=0.8,  # initial plot width fraction (0.25–0.85)
    resizable=True,            # show draggable splitter (default)
)

Set resizable=False for a fixed 80/20 split without a drag handle.

Supported media types

Extension Preview element
.png, .jpg, .jpeg, .gif, .webp, .svg <img>
.mp4, .webm, .ogg <video controls>
.html, .htm <iframe>
.pdf <iframe>
other fallback download link

Opening generated files

Local files (recommended for images)

  1. Generate the HTML with make_plot_interactive(...)
  2. Copy linked files next to the HTML if needed
  3. Double-click the .html file to open it in your browser

Images work with the file:// protocol.

When HTTP may be needed

Some browsers block file:// access for videos and embedded HTML/PDF in the preview panel. In that case only, use the optional local server below.

Optional HTTP server

Only needed if your browser refuses videos or iframes when opening files locally:

matplotlib-interactive serve output/
# open http://127.0.0.1:8000/plot.html

If the command is not found, activate your virtual environment first or run:

python -m matplotlib_interactive.cli serve output/

Options: --port, --host, --open.

Equivalent without this package:

cd output && python3 -m http.server 8000

API reference

make_plot_interactive

make_plot_interactive(
    fig,
    ax,
    x_data,
    y_data,
    labels,
    output_html_path,
    *,
    hotspot_radius_px=10,
    image_dpi=None,
    iframe_preview=None,
    theme=Theme(mode="auto"),
    show_hotspots=True,
)
Parameter Description
fig Matplotlib Figure
ax Matplotlib Axes for coordinate transforms
x_data, y_data Point coordinates in data space
labels One path or URL per point
output_html_path Output HTML path; PNG saved alongside
hotspot_radius_px Hotspot radius in pixels at native resolution
image_dpi DPI for saved PNG (default: fig.dpi)
iframe_preview Side panel configuration (IframePreview)
theme Page theme (Theme)
show_hotspots Show visible SVG hotspot markers (default: True)

IframePreview

Attribute Default Description
width, height None Fixed panel size; None enables auto layout
plot_width "80%" Max plot width when panel is closed
margin "16px" Layout margin in auto mode
initial_plot_ratio 0.8 Initial plot fraction when panel is open (0.25–0.85)
resizable True Show draggable splitter in auto layout
left, top, right, bottom Position overrides for fixed-size mode

Theme

Theme(mode="auto")   # follow system preference (default)
Theme(mode="light")
Theme(mode="dark")

detect_media_type

from matplotlib_interactive import detect_media_type

detect_media_type("clip.mp4")  # returns "video"

Development

pytest tests/ -m "not browser"
pytest tests/ -m browser

See examples/demo.py for a full preview-panel example.

Notes

  • Use a non-interactive backend such as Agg in scripts and CI.
  • The generated HTML and PNG are standalone files you can share as a folder.
  • show_hotspots and serve are independent: hotspots are on by default; the HTTP server is optional.

License

MIT License. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matplotlib_interactive_html-0.2.2.tar.gz (4.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matplotlib_interactive_html-0.2.2-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file matplotlib_interactive_html-0.2.2.tar.gz.

File metadata

File hashes

Hashes for matplotlib_interactive_html-0.2.2.tar.gz
Algorithm Hash digest
SHA256 406bf771563afb0f5dc332b8c8e3680bf913613754ab6728ffe4e9b16d142cf7
MD5 614b9ef0370409f052e515b9f7932eb0
BLAKE2b-256 c7537763085a6287526f73b541efe2fc54312db18d80181b197a2556ff736319

See more details on using hashes here.

File details

Details for the file matplotlib_interactive_html-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for matplotlib_interactive_html-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c982a469abbeb3e47224930ca7daf0cd3df407a77bf9fbe5333788500f0c773b
MD5 a22e9fe20e38a3c3eb3d2427dbca1238
BLAKE2b-256 3b4e771ee97d97d774d4d2e79f9e82edb6c5ef445c2b9741cd8152ac512c6421

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page