mpld3 is a Python package that adds interactivity to Matplotlib graphics, for enhanced visualization in browsers. It does so by producing [D3.js]( out of the Matplotlib figure.
In [1]:
# first the imports: the matplotlib usuals, plus mpld3
import numpy as np
import matplotlib.pyplot as plt
import mpld3
plt.style.use('bmh')
In [2]:
# Now we create a regular matplotlib figure, but we tell mpld3 to display it
plt.plot( [3,1,4,1,5], 'ks-', mec='w', mew=5, ms=20 )
mpld3.display()
# The resulting figure will have interactive pan/zoom controls (lower left)
Out[2]:
Another example, this one taken from the Scatter plot with tooltips example in the mpld3 gallery.
In [3]:
"""
Scatter Plot With Tooltips
==========================
A scatter-plot with tooltip labels on hover. Hover over the points to see
the point labels.
Use the toolbar buttons at the bottom-right of the plot to enable zooming
and panning, and to reset the view.
"""
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
N = 100
scatter = ax.scatter(np.random.normal(size=N),
np.random.normal(size=N),
c=np.random.random(size=N),
s=1000 * np.random.random(size=N),
alpha=0.3,
cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')
ax.set_title("Scatter Plot (with tooltips!)", size=20)
labels = ['point {0}'.format(i + 1) for i in range(N)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)
mpld3.display()
Out[3]:
In [ ]: