mpld3 Documentation

I want to take some notes on mpld3 to see if it is more practical to use / whether I should use it vs Bokeh.

1 30

References

The mpld3 project brings together Matplotlib, the popular Python-based graphing libraru, and D3js, the popular JavaScript library for creating interactive data visualizations for the web. The result is a simple API for exporting your matplotlib graphics to HTML code which can be used within the browser, within standard web pages, blogs, or tools such as the IPython notebook.

Quick Start Guide

The mpld3 package is extremely easy to use: you simply take any script generating a matplotlib plot, run it through one of mpld3's convenience routines, and embed the result in a web page. The current release of mpld3 can be installed with pip:

bash $ pip install mpld3

Then you can make an interactive plot like so:

python import matplotlib.pyplot as plt, mpld3 plt.plot([3,1,4,1,5], 'ks-', mec='w', mew=5, ms=20) mpld3.show()

These are the general functions used to convert matplotlib graphics into HTML and D3js.

Figures can be saved to file either in a stand-alone HTML format, or in a JSON format. mpld3 supplies the following convenience routines for this purpose:

The mpld3 plugin framework nearly endless possibilities for adding interactive behavior to matplotlib plots rendered in d3. The package includes several built-in plugins, which add zooming, panning, and other interactive behaviors to plots. Several examples of these plugins can be seen in the Example Gallery.

Installing mpld3

A core piece of the mpld3 package are the JavaScript libraries, which are located in the package in the mpld3/js/ directory. The mpld3.*.js is automatically constructed from a number of source JavaScript files; if you modify these sources, the libraries must be rebuilt before mpld3 is installed.

Frequently Asked Questions

Short answer: not really. Mpld3, like matplotlib itself, is designed for small to medium-scale visualizations, and this is unlikely to change. The reason is that mpld3 is built upon the foundation of HTML's SVGm which is not particularly well-suited for large datasets. Plots with more than a few thousand elements will have noticeably slow response for interactive features.

A list of unsupported features can be found with this GitHub link.

The client-side interface of mpld3 is a pure JavaScript library, which builds figures based on a well-defined JSON specification. This specification was designed with matplotlib in mind, but there's nothing stopping you from generating the JSON from another source.

At the moment, mpld3 only renders to SVG via D3, not to canvas.

There is a local copy of the mpld3 library bundled with the package, which you can find in mpld3/js/mpld3.v0.2/js where v0.2 indicates the library version,and matches the version of the mpld3 Python package.

To use mpld3 without an internet connection, you need to use a local version of the mpld3 and d3 libraries.

Comments

You have to be logged in to add a comment

User Comments

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language

Insert Chart

ESC

Use the search box below

Upload Previous Version of Article State

ESC