Making Python plots in web browser or iframe

2 minute read

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 HTML plotting , but full HTML plotting capability of Matplotlib requires mpld3. Virtually any Matplotlib plot can be converted to HTML for display in any web browser. As the name mpld3 hints, it’s HTML + D3.js (JavaScript) doing the plotting magic, but for simplicity we just say HTML.

Example 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()

The 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 mpld3 API.


Plotly full open-sourcing in November 2015 was immediately followed by the ability to make Plotly plots completely offline, without Plotly’s servers. Plotly version 1.9.4 from January 2016 make it possible to make offline Python plots that work in any web browser. Since then Plotly offline plots don’t need IPython/Jupyter, although you can certainly use them as well. Prior to Plotly offline mode, I rarely used Plotly due to fears they could shut off their service at any time, destroying my programs’ ability to plot. Offline plotting is important as it doesn’t rely on external proprietary services that could prevent future users from making plots, key to reproducible science. The Plotly API is available for Python, R, Matlab, JavaScript, Scala and a growing number of other programming languages.


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 plotly.offline.plot() as plotly.offline.iplot (iplot) will silently fail to do anything!



Leave a Comment