HTML plotting is important for communicating key data to colleagues, the general public and policymakers. HTML plots allow easy sharing of interactive data plots to any web browser. It’s important to science and engineering integrity to use completely open-source resources for HTML plot generation. In this way, years from now your plots are reproducible, a key element in reproducible science. While HTML plotting is important for every programming language, and indeed is trivially available for them, this article focuses on Python-based HTML data plotting.
There are numerous HTML plotting methods available from Python. Here are previews of a few of the most popular HTML Python plotting methods. These methods are completely open source (future reproducibility) and work in any web browser. The HTML file can be shared to any by several means, including * email attachment * embedded in a webpage as an HTML5 iframe * Ipython notebook * hosted plotting service (Plotly, figshare, et al)
Matplotlib 2.1 began to integrate
, but full HTML plotting capability of Matplotlib requires
Virtually any Matplotlib plot can be converted to HTML for display in any web browser.
As the name
mpld3.show() converts any Matplotlib plot to HTML and opens the figure in the web browser.
from matplotlib.pyplot import figure import mpld3 fig = figure() ax = fig.gca() ax.plot([1,2,3,4]) mpld3.show(fig)
mpld3.save_html(fig,'myfig.html') method saves figures to HTML files for archiving or uploading as an
iframe to a website, etc.
Many more useful D3.js enabled interactive elements are available from the
- Note the
.save_html(), which John notes in the comments below may help make plots show up.
The example plot shown above on this page was from Plotly examples
import plotly import plotly.graph_objs as go import numpy as np fn = 'tests/mt_bruno_elevation.csv' Z = np.loadtxt(fn,delimiter=',',skiprows=1,usecols=range(1,25)) data = [go.Surface(z = Z)] fig = go.Figure(data=data) plotly.offline.plot(fig, filename='elevations-3d-surface.html')
When using plain Python, be sure to use
iplot) will silently fail to do anything!