1. 绘制一个二维随机漫步的图形
直接上代码:
%pylab inline
nsteps = 1000
draws = np.random.randint(-1,2,size=(2,nsteps))
walks = draws.cumsum(1)
plot(walks[0,:],walks[1,:]);
Populating the interactive namespace from numpy and matplotlib
先生成 1000 个随机漫步方向,方向是从 {-1, 0, 1} 中随机挑两个值(两个值也可相等)作为移动方向,所以每次移动有 3×3=9 种选择,初始位置也是 9 种选择,cumsum 函数是将每次的移动累加,最后通过 plot 画出来。
2. 画出一个二次函数,同时画出梯形法求积分时的各个梯形
这里使用 IPython.html.widgets 模块中的 interact 函数,绘制一个交互式的函数图形。可以手动调整梯形个数,看到函数面积随梯形个数而变化。
def Quadratic(x):
return 2*x**2 +3*x +4
import numpy as np
import matplotlib.pyplot as plt
from IPython.html.widgets import interact
def plot_ladder(laddernum):
x = np.linspace(-5, 5, num=100)
y = Quadratic(x)
plot(x,y)
a = np.linspace(-5, 5, num=laddernum)
for i in range(laddernum):
plot([a[i],a[i]],[0,Quadratic(a[i])],color="black")
ladders = [];
for i in range(laddernum):
ladders.append([a[i],Quadratic(a[i])])
npladders = np.array(ladders)
plot(npladders[:,0],npladders[:,1]);
interact(plot_ladder, laddernum=(1, 30, 1))
3. 研究 IPython.html.widgets 模块中的 interact 函数,绘制一个交互式的函数图形
2014 年 4 月,IPython 增加了 interactive widgets,提供了可以交互的界面组件,如下例:
from IPython.html.widgets import interact
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
def plot_sine(n):
x = np.arange(0, 20, 0.1)
y = np.sin(x/n)
plot(x,y)
interact(plot_sine, n=(1, 30, 0.1))
还可以这样用。
from IPython.html.widgets import interact, IntSlider
from IPython.html.widgets import interact, IntSlider
def test_interact(min_, max_, steps_):
x = np.linspace(min_, max_, steps_)
y = np.sin(x)
z = np.cos(x)
plot(x, y)
scatter(x, y)
interact(test_interact,
min_=IntSlider(min=1,
max=10,
step=1,
value=1),
max_=IntSlider(min=10, max=20, step=1, value=10),
steps_=IntSlider(min=10, max=100, step=10, value=50))
4. Seaborn
Matplotlib 是 Python 主要的绘图库,但它本身很复杂,它的图经过大量的调整才能变精致。因此,作为替代,推荐使用Seaborn。Seaborn本质上使用Matplotlib作为核心库(就像Pandas对NumPy一样)。它可以:
- 默认情况下就能创建赏心悦目的图表。
- 创建具有统计意义的图。
- 能理解pandas的DataFrame类型,所以它们一起可以很好地工作。
import seaborn as sns
tips = sns.load_dataset("tips")
sns.jointplot("total_bill", "tip", tips, kind='reg');
参考资料