Mlab 基础
mayavi.mlab.show(func = None, stop = False)
基于numpy绘图
mlab对Numpy建立可视化过程
- 建立数据源
- 使用Filter(可选)
- 添加可视化模块
3D绘图函数
Points3d()
基于Numpy数组x、 y、 z提供的三维点坐标,绘制点图形(0D数据)
函数形式:
points3d(x, y, z…)
、points3d(x, y, z, s, …)
、points3d(x, y, z, f, …)
x,y,z 表示 numpy 数组、列表或者其他形式的点三维坐标
s 表示在该坐标点处的标量值
f 表示通过函数f(x,y,z)返回的标量值
参数
from mayavi import mlab import numpy as np t = np.linspace(0, 4*np.pi, 20) x = np.sin(2*t) y = np.cos(t) z = np.cos(2*t) s = 2 + np.sin(t) points = mlab.points3d(x, y, z, s, colormap = 'Reds', scale_factor = 0.25) mlab.show()
Plot3d()
基于1维Numpy数组x、 y、 z提供的三维坐标数据,绘制线
图形(1D数据)
from mayavi import mlab import numpy as np n_mer, n_long = 6, 11 dphi = np.pi / 1000.0 phi = np.arange(0.0, 2*np.pi + 0.5*dphi, dphi) mu = phi * n_mer x = np.cos(mu) + (1+np.cos(n_long*mu/n_mer)*0.5) y = np.sin(mu) + (1+np.cos(n_long*mu/n_mer)*0.5) z = np.sin(n_long * mu / n_mer) * 0.5 l = mlab.plot3d(x, y, z, np.sin(mu), tube_radius = 0.025, colormap = 'Spectral') mlab.show()
imshow()
from mayavi import mlab import numpy as np s = np.random.random((10, 10)) img = mlab.imshow(s, colormap = 'gist_earth') mlab.show()
surf()
from mayavi import mlab import numpy as np def f(x, y): return np.sin(x-y) + np.cos(x + y) x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05] s = mlab.surf(x, y, f) mlab.show()
contour_surf()
contour3d()
from mayavi import mlab import numpy as np x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j] scalars = x*x + y*y + z*z obj = mlab.contour3d(scalars, contours=8, transparent=True) mlab.show()
quiver3d()
import numpy as np from mayavi import mlab x, y, z = np.mgrid[-2:3, -2:3, -2:3] r = np.sqrt(x ** 2 + y ** 2 + z ** 4) u = y * np.sin(r)/(r + 0.001) v = -x * np.sin(r)/(r+0.001) w = np.zeros_like(z) obj = mlab.quiver3d(x, y, z, u, v, w, line_width=3, scale_factor=1) mlab.show()