Python plot HTML browser

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. There are numerous HTML plotting methods available from Python. These methods are completely open source and work in any web browser. The HTML file can be shared by several means:

  • email attachment
  • embedded in a webpage as an HTML5 iframe
  • Ipython notebook
  • hosted plotting service (Plotly, figshare, et al)

Matplotlib HTMLWriter can plot animated sequences. More powerful HTML plotting capability requires mpld3. Virtually any Matplotlib plot can be converted to HTML for display in any web browser. mpld3 uses HTML and D3.js to animate the plots.

from matplotlib.pyplot import figure
import mpld3

fig = figure()
ax = fig.gca()
ax.plot([1,2,3,4]) converts any Matplotlib plot to HTML and opens the figure in the web browser. mpld3.save_html(fig,'myfig.html') method saves figures to HTML files for archiving or uploading as an iframe to a website, etc. D3.js enabled interactive elements are available from the mpld3 API. Note the template_type='simple' keyword for .save_html(), which can increase robustness across web browsers.

The open source Plotly library can plot completely offline, without Plotly servers. Plotly can make offline Python plots that work in any web browser. Plotly offline plots don’t need IPython/Jupyter, although you can certainly use them as well.

Offline plotting is important as it doesn’t rely on external proprietary services that could prevent future users from making plots. The Plotly API is available for Python, R, Matlab, JavaScript, Scala and a growing number of other programming languages. Plotly examples show the simple syntax. When using plain Python, be sure to use plotly.offline.plot() as plotly.offline.iplot (iplot) will silently fail to do anything!