%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
n = 100
x = np.random.randn(n)
def update(curr_frm):
if curr_frm == n:
a.event_source.stop()
plt.cla()
bins = np.arange(-4, 4, 0.5)
plt.hist(x[:curr_frm], bins=bins)
plt.axis([-4, 4, 0, 30])
plt.gca().set_title('Sampling the Normal Distribution')
plt.gca().set_xlabel('Value')
plt.gca().set_ylabel('Frequency')
plt.annotate('n={}'.format(curr_frm), [3, 27])
fig = plt.figure()
# 100毫秒1帧
a = animation.FuncAnimation(fig, update, interval=100)
<IPython.core.display.Javascript object>
plt.figure()
data = np.random.rand(10)
plt.plot(data)
def onclick(event):
plt.cla()
plt.plot(data)
plt.gca().set_title('Pixels x={}, y={},\n data={}, {}'.format(event.x, event.y, event.xdata, event.ydata))
plt.gcf().canvas.mpl_connect('button_press_event', onclick)
<IPython.core.display.Javascript object>
7
from random import shuffle
import pandas as pd
countries = ['China', 'Australia', 'India', 'USA', 'Canada']
shuffle(countries)
df = pd.DataFrame({'height': np.random.rand(5),
'weight': np.random.rand(5),
'country': countries})
plt.figure()
plt.scatter(df['height'], df['weight'], picker=5)
plt.gca().set_ylabel('Weight')
plt.gca().set_xlabel('Height')
<IPython.core.display.Javascript object>
<matplotlib.text.Text at 0x982f240>
def on_pick(event):
country = df.iloc[event.ind[0]]['country']
plt.gca().set_title('From {}'.format(country))
plt.gcf().canvas.mpl_connect('pick_event', on_pick)
8