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.
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:
These are the general functions used to convert matplotlib graphics into HTML and D3js.
fig_to_html()
This is the core routine which takes a figure and constructs a string of HTML and JavaScript which can be embedded in any webpage
fig_to_dict()
This routine converts a matplotlib image to a JSON-serializable dictionary, which can be loaded into an appropraite HTML page and rendered via the mpld3 JavaScript library. Note the custom plugins which are not built into mpld3 will not be part of the JSON serialization.
show()
This function is mpld3's equivalent of matpltlib's plt.show function. It will convert the current figure to HTML using fig_to_d3(), start a local webserver which serves this HTML, and (if the operating system allows it) automatically open this page in the web browser.
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:
save_html()
Save a figure to a stand-alone HTML file
save_json()
Save the JSON representatoon of the figure to a file. Note that custom plugins which are not built into mpld3 will not be part of the JSON serialization.
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.
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.
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.
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.
Can mpld3 render to HTML5 canvas rather than SVG?
At the moment, mpld3 only renders to SVG via D3, not to canvas.
Where is the mpld3 JavaScript library located?
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.
How can I use mpld3 without an internet connection?
To use mpld3 without an internet connection, you need to use a local version of the mpld3 and d3 libraries.
Are you sure you want to delete this article section? You can not undo this change.
Add a Comment
Annotate Article
Copy Link to Selection
Share Article Selection
Ask AI About Selection
Write with AI
Rewrite Paragraph with AI
Rewrite with AI
A previous, saved version of this article is saved. To compare the current version of the article with the previous version - or to replace the current version with the saved version - click the icon above the text editor.
Successfully copied URL to clipboard!
Copied code to clipboard.
Copied TeX code to clipboard.
Uploaded file must be an image of type .jpeg, .jpg, .png, .webp, .gif, .bmp, or .svg.
Image Size must be less than 5MB.
Uploaded file must be an image of type jpeg, jpg, png, webp, avif, tiff, or svg.
Something went wrong uploading the image to the database. Try reloading the page.
The maximum number of images you can upload is 30.
There was an error taking an image using the device's camera. Try uploading an image instead.
Uploaded file must be an audio file.
Audio file must be an audio file of type .m4a, .flac, .mp3, .mp4, .wav, .wma, .aac, .webm, or .mpeg.
Audio file must be less than 300 MB in size.
Something went wrong uploading the audio file. Try reloading the page.
The maximum number of audio files you can upload is 10.
There was an error capturing an audio recording using the device's microphone. Try uploading an audio recording or video instead.
Uploaded file must be an video file.
Video file must be an audio file of type .mp4, .mov, .avi, .wmv, .avchd, .webm, or .flv.
Video file must be less than 300 MB in size.
Something went wrong uploading the video file. Try reloading the page.
The maximum number of video files you can upload is 10.
There was an error taking a video using the device's camera / microphone. Try uploading a video instead.
Comments
You can read more about how comments are sorted in this blog post.
User Comments
There are currently no comments for this article.