Writing Interactive Report with IPython

Receipts

IPython Notebook

The IPython Notebook is an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media

mpld3: A D3 Viewer for Matplotlib

This is an interactive D3js-based viewer which brings matplotlib graphics to the browser.

Shortcuts

Run Cell

Keyboard Action
Shift-Enter run cell
Ctrl-Enter run cell in-place
Alt-Enter run cell, insert below

Cope and Paste

Keyboard Action
Ctrl-m x cut cell
Ctrl-m c copy cell
Ctrl-m v paste cell

Delete and Insert

Keyboard Action
Ctrl-m d delete cell
Ctrl-m z undo last cell deletion
Ctrl-m – split cell
Ctrl-m a insert cell above
Ctrl-m b insert cell below

Toggle Output

Keyboard Action
Ctrl-m o toggle output
Ctrl-m O toggle output scroll
Ctrl-m l toggle line numbers

Save notebook, change cells

Keyboard Action
Ctrl-m s save notebook
Ctrl-m j move cell down
Ctrl-m k move cell up
Ctrl-m y code cell
Ctrl-m m markdown cell
Ctrl-m t raw cell
Ctrl-m 1-6 heading 1-6 cell
Ctrl-m p select previous
Ctrl-m n select next
Ctrl-m i interrupt kernel
Ctrl-m . restart kernel
Ctrl-m h show keyboard shortcuts

Example

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import mpld3
from mpld3 import plugins

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.ix[[i], :].T
 label.columns = ['Row {0}'.format(i)]
 # .to_html() is unicode; so make leading 'u' go away with str()
 labels.append(str(label.to_html()))

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

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('HTML tooltips', size=20)

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

mpld3.show()

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s