三流Mayavi操作-Mayav-2.1.0.6-volume_slice,flow绘制

秉着边学边写边折腾的原则,开始粗糙的工作。真正掌握还是得讲解给别人听。 先给出网课
https://www.icourse163.org/course/BIT-1001871001
Mayavi官方
http://docs.enthought.com/mayavi/mayavi/genindex.html
(有时候这网站会装死,一般过几个小时就会活过来)
我发现了,光是三流操作还不够,还得加上四流翻译。

这一章到基本函数绘制的最后了,flow,volume_slice也是最后两个。
这两个是比较有用的绘制函数,因为这个经常可以作为辅助观测平面。
这篇的讲法和以前一样,从官方例子引入,作必要的解释,然后翻译文档,这两个绘制比较特殊,因为它的参数和其他的都大不相同。
而且这两个绘制本身之间关系型也不是很强。

1.volume_slice

官方实例,微调

import numpy as np
from mayavi import mlab
x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalars = x * x * 0.5 + y * y + z * z * 2.0
mlab.volume_slice(scalars, plane_orientation='x_axes')
mlab.show()

仅靠这段代码是看不出来所以然的。因为作为例子的图是这样的

在这里插入图片描述
.
以上是原汁原味的官方实例

现在我给出补充后的代码来观测。
插入这句:
mlab.contour3d(scalars)

在这里插入图片描述

现在图像的原型基本出来了,绘制了它的等高面,而中间的观测平面是可以移动的。我们处理的时候采用的np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]进行离散,实际上,观测面又做了插值,把中间不存在的点又补上了。
这个面的好处在于,它是活动的,是可以交互地随意移动的一个面,注意到示例代码中:
mlab.volume_slice(scalars, plane_orientation='x_axes')
参数plane_orientation是x轴为法线作面,同理就还有'y_axes','z_axes'
volume_slice更多地是用来绘制辅助观察面~

2.flow

官方老代码。

import numpy as np
from mayavi.mlab import *
x, y, z = np.mgrid[-4:4:40j, -4:4:40j, 0:4:20j]
r = np.sqrt(x ** 2 + y ** 2 + z ** 2 + 0.1)
u = y * np.sin(r) / r
v = -x * np.sin(r) / r
w = np.ones_like(z)*0.05
contour3d(u, v, w)
obj = flow(u, v, w)
show()

下面的图都以这个代码为母体。

以及效果图

在这里插入图片描述

代码配置如下,这个技巧quiver3d是上一章内容,这个是矢量场的观测手段。
插入下面这句,把矢量场画出来。

quiver3d(u, v, w,mask_points=10)

同样也可以手动把矢量管线加进去,并降采样
在GUI里面对它设置On ratio=10可以设置得更高,每10个采集1个。如果不降采,矢量场会是密密麻麻的,虽然漂亮还是漂亮。
必要可以继续增设outline

在这里插入图片描述

然后一下就变得很绚丽了。sex,我很喜欢。

在这里插入图片描述

来看看详细解释

Creates a trajectory of particles following the flow of a vector field.
创建沿向量场流动的粒子轨迹。

这个翻译是有问题的,这个flow说得不清不楚的。trajectory本意是轨迹线,但是这里似乎倾向于说迹线。熟悉流体力学的应该知道流线(Streamline)迹线(Path line)的概念。而在管线配置中,最后的Module层级的默认标签是Streamline,这个是很明确的流线
在这里插入图片描述

Module层级表明是Streamline

在这里插入图片描述

语法

flow(u, v, w, ...)
flow(x, y, z, u, v, w, ...)
flow(x, y, z, f, ...)

u, v, w are numpy arrays giving the components of the vectors.
If only 3 arrays, u, v, and w are passed, they must be 3D arrays, and the positions of the arrows are assumed to be the indices of the corresponding points in the (u, v, w) arrays.
If 6 arrays, (x, y, z, u, v, w) are passed, the 3 first arrays give the position of the arrows, and the 3 last the components. The x, y and z arrays are then supposed to have been generated by numpy.mgrid, in other words, they are 3D arrays, with positions lying on a 3D orthogonal and regularly spaced grid with nearest neighbor in space matching nearest neighbor in the array. The function builds a vector field assuming the points are regularly spaced.
If 4 positional arguments, (x, y, z, f) are passed, the last one must be a callable, f, that returns vectors components (u, v, w) given the positions (x, y, z).
.
u, v, w为numpy arrays给定3个矢量分量。
如果只给定u, v, w 3 个参数,他们必须是3D array,且假定箭头的位置(u, v, w)匹配
如果给定(x, y, z, u, v, w) ,则前3个array 给定箭头位置,后3个给定矢量分量, x,y,z应当由numpy.mgrid来生成。换言之,他们是3维数组,且正交放置在规整的网格上,连通方式是空间上的就近连接。该函数将绘制矢量场,并假设了所有的点都是规整放置的。
如果给定4个参数 (x, y, z, f) ,根据位置(x, y, z),f必须可以返回位置对应的矢量分量(u, v, w)。

这里面有很多可以玩的。

点观测,线观测,面观测,空间观测
在这里插入图片描述

相应的设置在GUI里面,Source Widget里面,每一种观测对应的设置都不同,自己摸索吧。下面有很多好玩的。直接写进代码也是可以的见下面的参数seedtype

在这里插入图片描述

3.参数

在这里插入图片描述
除9个基本参数意外

flowvolume_slice共有参数:
extentfigure
.
extent

[xmin, xmax, ymin, ymax, zmin, zmax] Default is the x, y, z arrays extent. Use this to change the extent of the object created.
设置可视化的范围,默认是采用x,y,z数组的全部值域。使用这个可以改变创建的可视化对象范围。

这玩意会改可视化图像的数据,(不是改原始数据,可视化是映射成你看到的样子,他修改的是你看到,原始数据没有变化,后面我会说明这一点)进行整体放缩,在《mesh、triangular_mesh绘制》里面我写过这件事。我回去翻了一下好像不确定我说清楚没有,这里再给一个例子。我就用官方的代码来做例子。
这里稍微注意一下就是,添加了辅助手段xlabel('x')outline()来观测值域。

原图像:
注意值域范围是,x,y都是[0,39],z是[0,19]
在这里插入图片描述
.

x, y, z = np.mgrid[-4:4:40j, -4:4:40j, 0:4:20j]
r = np.sqrt(x ** 2 + y ** 2 + z ** 2 + 0.1)
u = y * np.sin(r) / r;v = -x * np.sin(r) / r;w = np.ones_like(z)*0.05
ext = [0,20,0,20,0,20]
quiver3d(u, v, w,mask_points=20,extent=ext)
xlabel('x')
outline()

在这里插入图片描述
注意看值域范围ext = [0,20,0,20,0,20]原始值域范围由outline()绘制出来,是[0,39,0,39,0,19],z方向已经被放大了,比19多1,横纵都压缩了接近一倍。

然后继续说前面那个问题,原始数据没有变化,注意代码的写法outline()xlabel('x')紧跟其后,默认添加在当前的数据源下面,也就是利用的同一个数据源

不妨打开管线OutlineVectors还有Axes属于同一个层级置于同一个数据源VectorScatter下面
如果数据源被修改了,那么OutlineVectors的范围会同时被修改。而上面看到的结果是,它还是按照数据本身的样子呈现。所以数据源是没有变化的。
在这里插入图片描述

其实如果仔细想过管线应该能马上想到这点,管线后面再说。
这个同样,追加了flow(u, v, w,extent=ext)sphere选定的在可视化区域外面,居然有数据,这就意味着计算机做了假图,HAHAHA。

在这里插入图片描述

最后就是,这个东西不是很常用。

figure

Figure to populate.
图像填充
简单点说就是图像基本设置在这里,背景什么的,可视化的框框大小。高级的话,这东西能抓Scenes管线。
mesh的的时候没注意到这个的用法,放这里展开一下。这里是具体的用法,本来想在讲管线的时候再讲这里的。戳进去,这里还有一个例子,我把它们抄过来。

Creates a new scene or retrieves an existing scene. If the mayavi engine is not running this also starts it.
创建新的场景scene或者检索已存在的scene,如果mayavi的engine失效了也可以通过它开启。

这里的sceneengine都是Mayavi的层级,在GUI里面只能看到Scenes级别以下的,engine是看不到的,这里不过多说管线,engine简单说就是管理Scenes的,Scene包含数据源,一层一层向下。

在这里插入图片描述

这个figure还是很有用的,涉及到engine的话就会有高级用法,这里也不讲,等到管线的时候讲。
mayavi.mlab.figure(figure=None, bgcolor=None, fgcolor=None, engine=None, size=(400, 350))
figure: The name of the figure, or handle to it.
bgcolor: The color of the background (None is default).
fgcolor: The color of the foreground, that is the color of all text annotation labels (axes, orientation axes, scalar bar labels). It should be sufficiently far from bgcolor to see the annotation texts. (None is default).
engine: The mayavi engine that controls the figure.
size: The size of the scene created, in pixels. May not apply for certain scene viewer.

语法来了,注意看语法基本都是缺省,抓取场景,设置前景背景和engine最后是设置图像大小。这里还是不展开,只讲bgcolor和fgcolor,对于我们绘图而言,比较实用的就是设置前景背景,下面举个栗子

figure(1, bgcolor=(1, 1, 1))
#figure(1,fgcolor=(1, 1, 1),size=(800,700)) #随便改啦

在这里插入图片描述

最后说一点,这里的flowquiver3d采用的是同一级数据源管线,figure是对应的Scenes是数据源的父层级,所以figure只用设置一次了。

flow独有:
integration_direction,linetype,scalars,seed_resolution,seed_scale,seed_visible,seedtype

integration_direction

The direction of the integration. Must be ‘forward’ or ‘backward’ or ‘both’. Default: forward
流线方向。只接受 ‘forward’ 或‘backward’或‘both’,其中forward是缺省值。
修改原来的代码,追加一个参数:
flow(u, v, w,integration_direction='both')
分别可以再设置'forward','backward',可以看到不同的效果
图示顺序是'both','forward','backward'

在这里插入图片描述

另外一种设置方式是用GUI:

在这里插入图片描述

linetype

the type of line-like object used to display the streamline. Must be 'line' or 'ribbon' or 'tube'. Default: line
用来展示流线的可视化对象的线型。只接受'line'(线型),'ribbon'(带状)或者'tube'(管状),'line'是默认缺省。
.
我个人喜欢管状,看着丰满。(对,我喜欢丰满的线条。咳咳。反正没人看写了无所谓)进入正题。
三种不同的线型,右下角是GUI设置。

在这里插入图片描述

scalars

optional scalar data.
设置标量,以前说过,这里给出一个例子。
修改官方老代码中的一行,把参数加上。(微调后的老代码不是原本的官方老代码)
flow(u, v, w,scalars=x+y-z,linetype='tube')
坐边是无scalars,右边追加了参数

在这里插入图片描述

注意像这种参数是没办法GUI的,只能写进代码。

seed_resolution

The resolution of the seed. Determines the number of seed points Must be an integer (int or long) or None.
seed的分辨率,决定seed的数目只接受integer (int 或者 long) 或者 None.
这个seed就是那个筛选器,前面说过筛选器可以是空间观测sphere,关键是这个筛选器的形状
修改老代码
flow(u, v, w,linetype='tube',seed_resolution=11)

在这里插入图片描述

不信可以数一下,就是11条半纬线。

seed_scale

Scales the seed around its default center Must be a float. Default: 1.0
默认对seed进行中心放缩,只接受float,默认1.0

不多说,就是放大,一个例子如下,大了很多。
flow(u, v, w,linetype='tube',seed_resolution=11,seed_scale=2.0)
在这里插入图片描述

seed_visible

Control the visibility of the seed. Must be a boolean. Default: True
控制seed是否可见,只接受布尔值。默认为True

一般用不到,连seed都隐藏了,就没办法交互了,除非seed遮挡了你的图。
在这里插入图片描述

seedtype

the widget used as a seed for the streamlines. Must be 'line' or 'plane' or'point' or sphere. Default:sphere
seed的观测类型设置。只接受'point','line','plane',sphere,默认是sphere
点观测'point',线观测'line',面观测'plane',空间观测sphere

前面举过例子,偷图。
在这里插入图片描述

.
.
下面的例子我用这个老代码,已调过一些参数

x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalars = x * x * 0.5 + y * y + z * z * 2.0
mlab.figure(size=(600,525))
mlab.contour3d(scalars,opacity=0.7,contours=3)
mlab.volume_slice(scalars)
mlab.show()

volume_slice独有:

plane_opacity

the opacity of the plane actor. Must be a legal value. Default: 1.0
对这个观测面设置透明度。只接受合法值。默认:1.0

这个和opacity参数一样范围0-1表示透明度,volume_slice也有opacity参数,后者是设置整个VTK对象的。但是实际上我都设过,是无效的。GUI里面也没有对应的选项。这个绘制要小心了。必要情况可以用transparent顶一下。

留坑。

plane_orientation

the orientation of the plane Must be a legal value. Default: x_axes
设置面的方向。必须是一个合法值。默认值是 x_axes
同理还有y_axes,z_axes

下面就y方向和z方向绘图,GUI里面也有相应的设置,可自行设置。
在这里插入图片描述

slice_index

The index along wich the image is sliced.
切片的索引号。
mlab.volume_slice(scalars,plane_orientation='y_axes')基础上追加

mlab.volume_slice(scalars,plane_orientation='y_axes',slice_index=15)
GUI也是可以直接设置的。这样可以确定一个精确的观测面,这个只接受整型数,毕竟是索引号。如果要浮点数可以从GUI的slice position进行修改
在这里插入图片描述

mlab.volume_slice(scalars,plane_orientation='y_axes',slice_index=90)
在这里插入图片描述

这个超过了倒是无所谓的。
np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]这句限制了,空间轴三个方向只能有64个面,有效索引是0-63,超过63都是按照最大索引处理。

填坑区:

1.plane_opacity'opacity'两个参数无效未解决。

更新(18.12.10已更完)
2018.12.10.——一次更完。

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值