8. Mayavi可视化实例

来源:转载

  • Dragon绘制
  • Canyon地形可视化
  • Earth Graph

Dragon绘制

import shutilimport tarfilefrom mayavi import mlabfrom os.path import join# 读取tar压缩文件dragon_tar_file = tarfile.open('Data/dragon.tar.gz')dragon_tar_file.extractall('dragon_data')dragon_tar_file.close()dragon_ply_file = join('dragon_data', 'dragon_recon', 'dragon_vrip.ply')# 渲染dragon ply文件mlab.pipeline.surface(mlab.pipeline.open(dragon_ply_file))mlab.show()# 删除解压的文件夹shutil.rmtree('Data/dragon_data')

Canyon地形可视化

import zipfileimport numpy as npfrom mayavi import mlab# 读取压缩文件# N36W113.hgt:北纬36-37度,西经113-114度 地形高程数据。hgt = zipfile.ZipFile('Data/N36W113.hgt.zip').read('N36W113.hgt')data = np.fromstring(hgt, '>i2')# 转换数据类型data.shape = (3601, 3601)data = data.astype(np.float32)# 选取一部分数据data = data[:1000, 900:1900]# data == -32768代表空缺值# 令空缺值等于最小值data[data == -32768] = data[data > 0].min()# 渲染地形hgt的数据datamlab.figure(size=(400, 320), bgcolor=(0.16, 0.28, 0.46))mlab.surf(data, colormap='gist_earth', warp_scale=0.2, vmin=1200, vmax=1610)# 清空内存del data# 创建交互式的可视化窗口mlab.view(-5.9, 83, 570, [5.3, 20, 238])mlab.show()

Earth Graph

import csvimport mathimport numpy as npfrom mayavi import mlabfrom mayavi.sources.builtin_surface import BuiltinSurface'''读取数据'''# 建立城市-城索引的字典、城市经纬度的列表cities = dict()coords = list()data = csv.reader(open('Data/cities.csv'))for line in data: name, longitude, latitude = line cities[name] = len(coords) coords.append((float(longitude), float(latitude)))'''坐标转换'''# 将经纬度的位置转换为三维坐标coords = np.array(coords)latitude, long = coords.T * np.pi / 180x = np.cos(long) * np.cos(latitude)y = np.cos(long) * np.sin(latitude)z = np.sin(long)'''建立窗口'''mlab.figure(bgcolor=(0.48, 0.48, 0.48), size=(800, 800))'''绘制地图'''# 绘制半透明球体表示地球sphere = mlab.points3d(0, 0, 0, scale_factor=2, color=(0.67, 0.77, 0.93), resolution=1024, opacity=0.7, name='Earth')# 调整镜面反射参数sphere.actor.property.specular = 0.45sphere.actor.property.specular_power = 5# 设置背面剔除,以更好的显示透明效果sphere.actor.property.backface_culling = True'''绘制城市'''# 绘制城市位置points = mlab.points3d(x, y, z, scale_mode='none', scale_factor=0.03, color=(0, 0, 1))# 绘制城市名称for city, index in cities.items(): label = mlab.text(x[index], y[index], city, z=z[index], color=(1, 1, 1), width=0.016 * len(city), name=city)'''绘制大洲边界'''continents_src = BuiltinSurface(source='earth', name='Continents')# 设置LOD为2# LOD: Levels-of-detailscontinents_src.data_source.on_ratio = 2continents = mlab.pipeline.surface(continents_src, color=(0, 0, 0))'''绘制赤道'''# 构造组成赤道线的数组theta = np.linspace(0, 2 * np.pi, 100) # 平分360为100份x = np.cos(theta)y = np.sin(theta)z = np.zeros_like(theta)# 绘制mlab.plot3d(x, y, z, color=(1, 1, 1), opacity=0.2, tube_radius=None)'''绘制两条纬线'''alpha = math.sqrt(3)/2mlab.plot3d(x * alpha, y * alpha, z + 0.5, color=(1, 1, 1), opacity=0.2, tube_radius=None)mlab.plot3d(x * alpha, y * alpha, z - 0.5, color=(1, 1, 1), opacity=0.2, tube_radius=None)'''显示可交互窗口'''# 设置相机及焦点位置mlab.view(100, 60, 4, [-0.05, 0, 0])# 显示窗口mlab.show()

分享给朋友:
您可能感兴趣的文章:
随机阅读: