Bringing Matplotlib to the Browser

<no title>

«  <no title>   ::   Contents   ::   <no title>  »

Python source code: [download source:]

HTML tooltip plugin
This is a demonstration of how to add rich HTML annotations to data plots.
The Plugin is defined within mpld3, and the user-provided CSS controls the
format of the information shown on hover.
Use the toolbar buttons at the bottom-right of the plot to enable zooming
and panning, and to reset the view.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import mpld3
from mpld3 import plugins

# Define some CSS to control our custom labels
css = """
  border-collapse: collapse;
  color: #ffffff;
  background-color: #000000;
  background-color: #cccccc;
table, th, td
  font-family:Arial, Helvetica, sans-serif;
  border: 1px solid black;
  text-align: right;

fig, ax = plt.subplots()
ax.grid(True, alpha=0.3)

N = 50
df = pd.DataFrame(index=range(N))
df['x'] = np.random.randn(N)
df['y'] = np.random.randn(N)
df['z'] = np.random.randn(N)

labels = []
for i in range(N):
    label = df.iloc[[i], :].T
    label.columns = ['Row {0}'.format(i)]
    # .to_html() is unicode; so make leading 'u' go away with str()

points = ax.plot(df.x, df.y, 'o', color='b',
                 mec='k', ms=15, mew=1, alpha=.6)

ax.set_title('HTML tooltips', size=20)

tooltip = plugins.PointHTMLTooltip(points[0], labels,
                                   voffset=10, hoffset=10, css=css)
plugins.connect(fig, tooltip)

«  <no title>   ::   Contents   ::   <no title>  »