D3 Viewer for Matplotlib
mpld3, currently at version 0.5.12, bridges Matplotlib's powerful plotting capabilities with D3.js for interactive web visualizations. It provides a straightforward API to export Matplotlib graphics to HTML, suitable for web pages, blogs, or Jupyter notebooks. The project's development is in maintenance mode, with maintainers reviewing pull requests but having limited time for issue resolution.
Warnings
- gotcha When updating mpld3, particularly in Jupyter Notebooks, cached JavaScript libraries can cause unexpected behavior. The notebook might still use older versions of the JS library, even if the Python package is updated.
- gotcha mpld3 is not designed for large datasets (plots with more than a few thousand elements). Performance for interactive features will degrade significantly due to its reliance on HTML SVG, which is not optimized for big data visualization.
- deprecated Importing mpld3 can trigger numerous deprecation warnings from `ipywidgets` in newer Python environments (e.g., Python 3.5+). This is due to broad warning filters applied by mpld3's internal `utils.py` module.
- gotcha mpld3's compatibility documentation explicitly mentions Python versions 2.6-2.7 and 3.3-3.4. While it may run on newer Python 3 versions (e.g., 3.5+), these versions are not officially guaranteed to be fully supported or tested, which could lead to unexpected behavior or incompatibilities.
- gotcha Some advanced Matplotlib features, such as custom widgets or intricate tick label formatting (e.g., `ax.xaxis.set_ticklabels()`), may not be fully supported or accurately translated into the D3.js output, limiting full interactive parity with native Matplotlib figures.
Install
-
pip install mpld3 -
conda install mpld3
Imports
- mpld3
import mpld3
- matplotlib.pyplot
import matplotlib.pyplot as plt
Quickstart
import matplotlib.pyplot as plt
import mpld3
fig, ax = plt.subplots()
ax.plot([3, 1, 4, 1, 5], 'ks-', mec='w', mew=5, ms=20)
ax.set_title("My Interactive Plot")
# To display in a browser:
mpld3.show(fig)
# To get HTML string:
# html_output = mpld3.fig_to_html(fig)
# print(html_output)