Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi

Introduction

** In research that makes full use of scientific and technical calculations, the isosurface of scalar functions is often investigated. ** ** Doing this with standard plots using matplotlib alone may not be easy (or perhaps not possible at this time).

** On the other hand, using mayavi allows you to quickly draw an isosurface. mayavi is (apparently) known as an excellent 3D visualization library for presentations, conference presentations, and dissertations [1]. ** **

Here, as an example of drawing the isosurface using mayavi, the isosurface of the trigonometric function $ f (x, y, z) = \ frac {\ sin (xyz)} {(xyz)} \ tag 1 $ Try to draw [1,2].

A lot of information is posted on mayavi's official website, and this article follows it.


Contents

(1) Draw four isosurfaces by dividing the possible values of f into four equal parts.

(2) Draw a cross section of the isosurface.

Code (1) Draw an isosurface


"""
Draw a 3D isosurface using mayavi: iso-surface
30 Aug. 2017
"""
import numpy as np
from mayavi import mlab
mlab.init_notebook()  #Notebook initialization. Be sure to put it in.

x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalar = np.sin(x*y*z)/(x*y*z)  #Function settings

#Generation of drawing area
mlab.figure(1, bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(600, 400)) #Set background color, size, etc.
mlab.clf()

#Draw a 3D isosurface. Set colormap to jet, contours is the number of divisions, line_width is line thickness, opacity is opacity(The default is 1.0)
obj=mlab.contour3d(scalar,colormap='jet',\
                   contours=4,line_width=1.3,opacity=0.8) 

mlab.show()

obj   #Finally run the drawing object! If you don't do this, nothing happens.

result

スクリーンショット 2017-08-30 18.26.02.png

Code (2) Draw a cross section of the isosurface

"""
Draw a cross section of the 3D isosurface using mayavi: 
"""
import numpy as np
from mayavi import mlab
mlab.init_notebook()

x, y, z = np.ogrid[-5:5:64j, -5:5:64j, -5:5:64j]
scalar = np.sin(x*y*z)/(x*y*z)

#Cross section setting:Create objects with cross sections parallel to the x-axis and y-axis with the names ss1 and ss2
ss1=mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(scalar),plane_orientation='x_axes',slice_index=10,)
ss2=mlab.pipeline.image_plane_widget(mlab.pipeline.scalar_field(scalar),plane_orientation='y_axes',slice_index=10,)

mlab.show()

ss1
ss2

Result (2): Cross section of the isosurface

スクリーンショット 2017-08-30 18.33.39.png


Addendum:

(1) Installation of mayavi using conda (see [3])

My machine environment: macOS Sierra 10.12.6 Start the terminal and

  1. conda install -c menpo mayavi=4.5.0
  2. jupyter nbextension install --py mayavi --user
  3. conda install pyQT

And said.

(2) When using macOS + Jupyter

When starting Jupyter-notebook 5.0 or later, ** iopub_data_rate_limit must be set large, otherwise the memory required for drawing using mayavi will be insufficient **. Therefore, when starting jupyter, it seems necessary to set *** jupyter-notebook --NotebookApp.iopub_data_rate_limit = 10000000000 *** etc. [4].


References

[1] miyavi tutorial: http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html [2] About contour3d (English) http://mdns.esy.es/2017/06/17/contour3d/

[3] Qiita article by 2dod, Settings when using Mayavi with Jupyter notebook [4] Regarding iopub_data_rate_limit (English): https://github.com/jupyter/notebook/issues/2287

Recommended Posts

Scientific / technical calculation by Python] Drawing and visualization of 3D isosurface and its cross section using mayavi
[Scientific / technical calculation by Python] Drawing of 3D curved surface, surface, wireframe, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing, visualization, matplotlib of 2D (color) contour lines, etc.
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
[Scientific and technical calculation by Python] Drawing of fractal figures [Sierpinski triangle, Bernsley fern, fractal tree]
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
[Scientific / technical calculation by Python] Drawing animation of parabolic motion with locus, matplotlib
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib
[Scientific / technical calculation by Python] Basic operation of arrays, numpy
[Scientific / technical calculation by Python] List of usage of (special) functions used in physics by using scipy
[Scientific / technical calculation by Python] Wave "beat" and group velocity, wave superposition, visualization, high school physics
[Scientific / technical calculation by Python] Vector field visualization example, electrostatic field, matplotlib
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation
[Scientific / technical calculation by Python] Monte Carlo simulation of thermodynamics of 2D Ising spin system by Metropolis method
[Scientific / technical calculation by Python] Sum calculation, numerical calculation
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
[Scientific / technical calculation by Python] Calculation of matrix product by @ operator, python3.5 or later, numpy
[Scientific / technical calculation by Python] Derivation of analytical solutions for quadratic and cubic equations, mathematical formulas, sympy
[Scientific / technical calculation by Python] Lagrange interpolation, numerical calculation
Calculation of technical indicators by TA-Lib and pandas
[Scientific / technical calculation by Python] Solving (generalized) eigenvalue problem using numpy / scipy, using library
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
[Scientific / technical calculation by Python] Plot, visualize, matplotlib 2D data with error bars
Analysis of financial data by pandas and its visualization (2)
Analysis of financial data by pandas and its visualization (1)
[Scientific / technical calculation by Python] Numerical solution of one-dimensional and two-dimensional wave equations by FTCS method (explicit method), hyperbolic partial differential equations
[Scientific / technical calculation by Python] Numerical solution of second-order ordinary differential equations, initial value problem, numerical calculation
[Scientific / technical calculation by Python] List of matrices that appear in Hinpan in numerical linear algebra
[Scientific / technical calculation by Python] Numerical solution of one-dimensional harmonic oscillator problem by velocity Verlet method
[Scientific / technical calculation by Python] Numerical solution of eigenvalue problem of matrix by power method, numerical linear algebra
[Scientific / technical calculation by Python] 3rd order spline interpolation, scipy
[Scientific / technical calculation by Python] Monte Carlo integration, numerical calculation, numpy
[Scientific / technical calculation by Python] Numerical integration, trapezoidal / Simpson law, numerical calculation, scipy
[Scientific / technical calculation by Python] 1D-3D discrete fast Fourier transform, scipy
[Scientific / technical calculation by Python] Numerical solution of one-dimensional unsteady heat conduction equation by Crank-Nicholson method (implicit method) and FTCS method (positive solution method), parabolic partial differential equation
Clustering and visualization using Python and CytoScape
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
[Control engineering] Calculation of transfer functions and state space models by Python
[Scientific / technical calculation by Python] Solving the boundary value problem of ordinary differential equations in matrix format, numerical calculation
Example of 3D skeleton analysis by Python
Regression model and its visualization using scikit-learn
[Scientific / technical calculation by Python] Solving second-order ordinary differential equations by Numerov method, numerical calculation
[Scientific / technical calculation by Python] Solving one-dimensional Newton equation by the 4th-order Runge-Kutta method
[Scientific / technical calculation by Python] Generation of non-uniform random numbers giving a given probability density function, Monte Carlo simulation