Catalog
3D contour plot via Mayavi

Recently I am interested in the data visualization and I have try different plots. Previously in my research work I ploted a 3d contour figure via MATLAB. Since the frequently used Python package Matplotlib has poor support on this kind of 3d data visualization. I also tried the Mayavi package before and I didn’t learn it well.

After reading a lot of examples I begin to control the skill of using Mayavi. I try to replot the original plots via python.

Here is the process. We first need import the necessary package

1
import numpy as np
2
from mayavi import mlab

Then the 3d data should be prepared. The data are calculated via parameter sweep and store with txt format. We need load it

1
# %% Load the data
2
dis_tri = np.loadtxt('../3D_Animation/BigRangeSweep3Cav.txt')
3
# %%
4
num_J12 = 199
5
num_J23 = 200
6
num_kappa = 201
7
J12_mat = np.linspace(1, 9, num_J12)
8
J23_mat = np.linspace(1, 10, num_J23)
9
kappa_mat = np.linspace(1, 14, num_kappa)
10
11
xx, yy, zz = np.mgrid[1:14:201j, 1:10:200j, 1:9:199j]
12
sym_mat = np.reshape(dis_tri, (num_kappa, num_J23, num_J12))

We need transpose the matrix since I need kappa to be the z axi

1
sym_mat_tr = sym_mat.transpose(1, 2, 0)
2
xx_tr, yy_tr, zz_tr = np.mgrid[1:10:200j, 1:9:199j, 1:14:201j]

Then we can plot the 3d data

1
fig_extent = [1, 10, 1, 9, 1, 14]
2
fig1 = mlab.figure(size=(1000, 800), bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
3
s = mlab.contour3d(xx_tr, yy_tr, zz_tr, sym_mat_tr, color=(0, 0.7, 0.7), contours=[
4
    0.8], transparent=True, opacity=0.15)  # ,extent=fig_extent)
5
mlab.contour3d(xx_tr, yy_tr, zz_tr, sym_mat_tr, color=(0, 0, 1), contours=[
6
               0.9], transparent=True, opacity=0.3)  # ,extent=fig_extent)
7
mlab.contour3d(xx_tr, yy_tr, zz_tr, sym_mat_tr, color=(1, 0, 0), contours=[
8
               0.97], transparent=True, opacity=0.4)  # ,extent=fig_extent)
9
mlab.outline(extent=fig_extent)
10
mlab.xlabel('J23/g')
11
mlab.ylabel('J12/g')
12
mlab.zlabel('K/2g')
13
mlab.view(180 + 45, -70, 42, [5.5, 5, 7.5])
14
mlab.axes(color=(1, 0, 0), nb_labels=5)
15
mlab.savefig('sym_distri_static_Python.png')
16
mlab.show()

I have used the function contour3d and its usage can be found in the mayavi doc web.

The following is the figure I plotted.

3d Contour plots

Author: Knifelee
Link: https://knifelees3.github.io/2019/12/30/A_En_Python_PlotStudy5_Contour3dPlots/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶

Comment