实验目的
掌握Python语言中统计作图函数的用法
实验原理
通过统计作图函数绘制的图标可以直观地反映出数据及统计量地性质及其内在规律,如盒图可以表示多个样本地均值,误差条形图能同时显示下限误差和上限误差,最小二乘拟合曲线图能分析两变量间的关系。
实验步骤
注意:python2中print语句不加括号,python3中print语句打印内容要加括号!
普通图
- `from pylab import * `
- ` n = 256 `
- `X = np.linspace(-np.pi,np.pi,n,endpoint=True) `
- `Y = np.sin(2*X) `
- `plot (X, Y+1, color='blue', alpha=1.00) `
- `plot (X, Y-1, color='blue', alpha=1.00) `
show()
散点图
- `from pylab import * `
- `n = 1024 `
- `X = np.random.normal(0,1,n) `
- `Y = np.random.normal(0,1,n) `
- `scatter(X,Y) `
show()
条形图
- `from pylab import * `
- `n = 12 `
- `X = np.arange(n) `
- `Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) `
- `Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) `
- `bar(X, +Y1, facecolor='#9999ff', edgecolor='white') `
- `bar(X, -Y2, facecolor='#ff9999', edgecolor='white') `
- `for x,y in zip(X,Y1): `
- ` text(x+0.4, y+0.05, '%.2f' % y, ha='center', va= 'bottom') `
- `ylim(-1.25,+1.25) `
show()
等高线图
- `from pylab import * `
- `def f(x,y): return (1-x/2+x5+y3)_np.exp(-x*2-y_2) `
- `n = 256 `
- `x = np.linspace(-3,3,n) `
- `y = np.linspace(-3,3,n) `
- `X,Y = np.meshgrid(x,y) `
- `contourf(X, Y, f(X,Y), 8, alpha=.75, cmap='jet') `
- `C = contour(X, Y, f(X,Y), 8, colors='black', linewidth=.5) `
show()
灰度图(Imshow)
- `from pylab import * `
- ` def f(x,y): return (1-x/2+x5+y3)_np.exp(-x*2-y_2) `
- ` n = 10 `
- `x = np.linspace(-3,3,4*n) `
- `y = np.linspace(-3,3,3*n) `
- `X,Y = np.meshgrid(x,y) `
imshow(f(X,Y)), show()
饼状图
- `from pylab import * `
- ` `
- `n = 20 `
- `Z = np.random.uniform(0,1,n) `
pie(Z), show()
量场图(Quiver Plots)
- `from pylab import * `
- ` `
- `n = 8 `
- `X,Y = np.mgrid[0:n,0:n] `
quiver(X,Y), show()
3D 图
- `from pylab import * `
- `from mpl_toolkits.mplot3d import Axes3D `
- ` fig = figure() `
- `ax = Axes3D(fig) `
- `X = np.arange(-4, 4, 0.25) `
- `Y = np.arange(-4, 4, 0.25) `
- `X, Y = np.meshgrid(X, Y) `
- `R = np.sqrt(X2 + Y2) `
- `Z = np.sin(R) `
- ` ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot') `
- ` show()`