三流Mayavi操作-Mayav-2.1.4-surf、contour_surf、contour3d绘制

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

2.0.简述(1)

2.1 .mlab图形可视化,图像操作

2.1.0 绘图函数

2.1.0.0 绘图函数,通用参数
2.1.0.1 .barchart
2.1.0.2 .mesh,.triangular_mesh
2.1.0.3 .imshow,.plot3d
2.1.0.4 .surf,.contour_surf,.contour3d:*当前位置
2.1.0.5.points3d,.quiver3d
2.1.0.6.flow,.volume_slice
2.1.0.7 补充、对比及总结

2.1.1 图像控制

2.1.1.0

2.1.2 图像修饰
2.1.3 相机控制
2.1.4 其他

2.2.api操作管线对象,窗口对象

2.2.1 脚本录制

这一章主要讲surfcontour_surfcontour3d绘制
先说surf,因为经常要用,而且前面提到过,说起来方便。然后contour_surf,最后是contour3d

1.surf和contour_surf

官方实例,官方也是很友善,把代码合并在一起了。

import numpy as np
from mayavi.mlab import *
def f(x, y):
    sin, cos = np.sin, np.cos
    return sin(x + y) + sin(2 * x - y) + cos(3 * x + 4 * y)
x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
surf(x, y, f)
contour_surf(x, y, f)
show()

以下是效果图

在这里插入图片描述
官方还是很良心的制作了一个,第一个图是surf第二个是contour_surf最后那个是两个合并的可视化,这么做不好,因为是我干的,为了省事。
其实我只是想说,两者可以叠加,管线会出现两个层级,还可以继续叠加outline

前面说了meshsurf的区别,在于绘制的网格的不同,这里再说一次,mesh的绘制方式是三角形网格,surf是规整的网格

在这里插入图片描述

surf实际是正交,我在这里增设了一个面,这个面是整个绘制范围,并设置了opacity,是不是秒懂。(我这里的面设置的高度是1,角度看起来就有点奇怪,设0会好点)

在这里插入图片描述

对于surfcontour_surf的简单介绍如下:

surf:Plots a surface using regularly-spaced elevation data supplied as a 2D array.
contour_surf:Plots a the contours of a surface using grid-spaced data for elevation supplied as a 2D array.

利用形如2Darray的高程数据绘制具有规整网格的平面
语法如下(两者语法相同)

surf(s, ...)
surf(x, y, s, ...)
surf(x, y, f, ...)

详细说明(也是相同的,措辞微微调整了一下,这里给出surf(s, ...)的全文翻译(误))

s is the elevation matrix, a 2D array, where indices along the first array axis represent x locations, and indices along the second array axis represent y locations.
.
x and y can be 1D or 2D arrays such as returned by numpy.ogrid or numpy.mgrid. Arrays returned by numpy.meshgrid require a transpose first to obtain correct indexing order. The points should be located on an orthogonal grid (possibly non-uniform). In other words, all the points sharing a same index in the s array need to have the same x or y value. For arbitrary-shaped position arrays (non-orthogonal grids), see the mesh function.
.
If only 1 array s is passed, the x and y arrays are assumed to be made from the indices of arrays, and an uniformly-spaced data set is created.
If 3 positional arguments are passed the last one must be an array s, or a callable, f, that returns an array. x and y give the coordinates of positions corresponding to the s values.

s是一个高程矩阵,2维array的形式,其中第一个位置代表x轴,第二个位置是y轴。
.
x,y可以是由numpy.ogrid或者numpy.mgrid初始的1维或者2维array.为了得到正确的索引顺序,被numpy.meshgrid返回的array的第一个array会发生转置.这些点会被放置在正交网格上(可能不均匀).s中的每一个值所匹配x,y的坐标应该具备相同的索引.对于不规则的array而言(非正交网格),可以参考mesh.
.
可以只传递1个参数s,x,y的索引已经预设好,这时就创建了一个等间距的数据集。
也可以传递3个参数,最后一个参数必须是一个array,或者是一个可返回的f表达式,且返回的类型也是array,s值的坐标由前两个参数x,y给出。
.
contour_surf

s is the elevation matrix, a 2D array. The contour lines plotted are lines of equal s value.
.
x and y can be 1D or 2D arrays (such as returned by numpy.ogrid or numpy.mgrid), but the points should be located on an orthogonal grid (possibly non-uniform). In other words, all the points sharing a same index in the s array need to have the same x or y value. For arbitrary-shaped position arrays (non-orthogonal grids), see the mesh function.
.
If only 1 array s is passed, the x and y arrays are assumed to be made from the indices of arrays, and an uniformly-spaced data set is created.
.
If 3 positional arguments are passed the last one must be an array s, or a callable, f, that returns an array. x and y give the coordinates of positions corresponding to the s values.

官方实例用的就是三个参数的形式.先离散再用绘制,surfcontour_surf传参方式相同。
x, y = np.mgrid[-7.:7.05:0.1, -5.:5.05:0.05]
surf(x, y, f)

然后呢,我想提一下管线

在这里插入图片描述
surfcontour_surf的配置很相似,上面是surf下面是contour_surf
surf多了一层PolyDataNormals,这里先把问题抛出来,然后讲一点管线。

为什么在这里要讲一下管线配置,因为官方用的就是surf做的例子,满满的幸福感,全程翻译。

The plotting functions reviewed above explore only a small fraction of the visualization possibilities of Mayavi. The full power of Mayavi can only be unleashed through the control of the pipeline itself. As described in the An overview of Mayavi section, a visualization in Mayavi is created by loading the data in Mayavi with data source object, optionally transforming the data through Filters, and visualizing it with Modules. The mlab functions build complex pipelines for you in one function, making the right choice of sources, filters, and modules, but they cannot explore all the possible combinations.

前面涉及到的绘图函数只探索了Mayavi可视化的一小部分.只有通过管线配置才能充分发挥Mayavi的全部效果。在 An overview of Mayavi章节的叙述中,Mayavi 可以通过配置数据源来创建可视化实例,在这个过程中,可以使用Filters任意变换数据,并通过 Modules进行可视化。mlab函数可以用一条命令生成复杂的管线,并配置正确的数据源sources, filters, 和 modules,但是他们无法同时探索所有的可能正确的组合形式。
.
Mlab provides a sub-module pipeline which contains functions to populate the pipeline easily from scripts. This module is accessible in mlab: mlab.pipeline, or can be import from mayavi.tools.pipeline.
Mlab提供一个子模块管线,它包含很多用于配置管线的函数,通过脚本可以轻易实现。这个模块可以在mlab中引用,通过mlab.pipeline或者mayavi.tools.pipeline
.
When using a mlab plotting function, a pipeline is created: first a source is created from numpy arrays, then modules, and possibly filters, are added. The resulting pipeline can be seen for instance with the mlab.show_pipeline command. This information can be used to create the very same pipeline directly using the pipeline scripting module, as the names of the functions required to create each step of the pipeline are directly linked to the default names of the objects created by mlab on the pipeline. As an example, let us create a visualization using surf():
当调用mlab函数的时候,一条pipeline管线已经创建好了:首先是由numpy arrays初始的source,然后是modules,可能中间还会有filters。最后生成的pipeline管线可以被mlab.show_pipeline命令显示出来。通过pipeline管线的脚本模块,这些信息可以用来创建相同的管线,根据名称,可以直接通过object对象的默认名,直接创建管线的每一步配置。作为例子,我们可以用surf()做可视化:

.

import numpy as np
a = np.random.random((4, 4))
from mayavi import mlab
mlab.surf(a)
mlab.show_pipeline()

The following pipeline is created:
Array2DSource
—— WarpScalar
————PolyDataNormals
——————Colors and legends
———————— Surface
在这里插入图片描述

The same pipeline can be created using the following code:
相同的管线,也可以用以下代码进行创建

import numpy as np
a = np.random.random((4, 4))
from mayavi import mlab
src = mlab.pipeline.array2d_source(a)
warp = mlab.pipeline.warp_scalar(src)
normals = mlab.pipeline.poly_data_normals(warp)
surf = mlab.pipeline.surface(normals)


吃个饭未完待续

bac54