pyodide / matplotlib-pyodide

HTML5 backends for Matplotlib compatible with Pyodide
Mozilla Public License 2.0
31 stars 9 forks source link

Use matplotlib figures externally #30

Open CesMak opened 1 year ago

CesMak commented 1 year ago

Hey there, Is it possible to generate the figures including first and then use them on a static webpage without any python or matplotlib? Including hovering over the points and zooming in? I tried it already, I think the problem here is that the data is stored in events: image

Thanks Markus

rajsite commented 1 year ago

Interesting question! I don't think matplotlib has that capability built-in.

Maybe you can use a library like https://github.com/mpld3/mpld3?

The following seems to run in the pyodide console:

import matplotlib
from matplotlib import pyplot as plt
import numpy as np

matplotlib.use('Agg')

import micropip
await micropip.install("mpld3")
import mpld3

x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
out = mpld3.fig_to_html(fig)
out

The output is truncated in the online pyodide console, but you could save the string output in js to use