Skip to content

Convert matplotlib quiver plots - #5843

Open
robertoffmoura wants to merge 16 commits into
plotly:mainfrom
robertoffmoura:rm/support-quiver-plots
Open

robertoffmoura wants to merge 16 commits into
plotly:mainfrom
robertoffmoura:rm/support-quiver-plots

Conversation

@robertoffmoura

Copy link
Copy Markdown
Contributor

mpl_to_plotly drops the arrows of matplotlib quiver plots. plt.quiver creates a Quiver collection, which the exporter passes to draw_path_collection like any other path collection. The renderer turns it into a scatter of circle markers at the arrow positions with an empty marker size list, so the converted figure shows empty axes.

Fix: draw_path_collection now draws Quiver collections as plotly quiver traces (added in plotly.js 4.0.0, bundled since plotly.py 7.0.0):

  • Each arrow's tail and tip are read from matplotlib's arrow path, so arrow lengths, directions and pivots ('tail', 'mid', 'tip') follow the matplotlib figure. Each arrow is anchored at its tail, with u/v reaching its tip in axis units: data units on linear axes, decades on log axes and milliseconds on date axes.
  • Arrow positions given in data, axes, figure or display coordinates convert, on linear, log and date axes.
  • Arrows of each color go into their own trace, so per-arrow and colormapped colors are kept. Arrows drawn only as outlines (facecolor="none") take the edge color.
  • Masked and NaN arrows are skipped.

Snippet to reproduce:

import datetime
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import plotly.tools as tls


def save_comparison(fig, name):
    fig.savefig(f"{name}_mpl.png", bbox_inches="tight")
    p = tls.mpl_to_plotly(fig)
    p.write_image(f"{name}_plotly.png")


# 1. Vector field
x = np.arange(-3, 3.5, 0.5)
X, Y = np.meshgrid(x, x)
fig1, ax1 = plt.subplots()
ax1.quiver(X, Y, np.sin(X), np.cos(Y))
save_comparison(fig1, "quiver_field")

# 2. Colormapped arrows
x = np.linspace(-2, 2, 8)
X, Y = np.meshgrid(x, x)
fig2, ax2 = plt.subplots()
ax2.quiver(X, Y, -Y, X, Y + X / 8)
save_comparison(fig2, "quiver_colormapped")
matplotlib plotly before plotly after
quiver_field_mpl quiver_field_plotly_before quiver_field_plotly_after
quiver_colormapped_mpl quiver_colormapped_plotly_before quiver_colormapped_plotly_after

@robertoffmoura robertoffmoura changed the title Rm/support quiver plots Convert matplotlib quiver plots Oct 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant