Catalog
A rotated 3D contour plot via Mayavi

This figure is the Fig2 of my research work and I let it rotated.

This time I try o plot the 3d animated distribution via python package Mayavi

Different from the animated operation via using the Matplotlib, This 3d data visualization is more powerful.

I only upload the figure this time and I will write a complete note latterly.

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. The static plot could be plotted first

1
fig_extent = [1, 10, 1, 9, 1, 14]
2
fig1 = mlab.figure(size=(800, 800), bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))
3
mlab.contour3d(xx, yy, zz, sym_mat, color=(0, 0.3, 0.3), contours=[
4
    0.8], transparent=True, opacity=0.15)  # ,extent=fig_extent)
5
mlab.contour3d(xx, yy, zz, sym_mat, color=(0, 0.7, 0.7), contours=[
6
               0.9], transparent=True, opacity=0.3)  # ,extent=fig_extent)
7
mlab.contour3d(xx, yy, zz, sym_mat, color=(0, 0, 0.8), contours=[
8
               0.97], transparent=True, opacity=0.4)  # ,extent=fig_extent)
9
mlab.outline(extent=fig_extent)
10
mlab.axes(color=(1, 0, 0), nb_labels=5)

then we should define a animated function. Since in this case we only need update the view rather than the data. So the code is a little bit diffeent

1
def make_frame(t):
2
    # camera angle
3
    mlab.view(azimuth=360 * t / duration, elevation=-70, distance=42, focalpoint=[5.5, 5, 7.5])
4
    return mlab.screenshot(antialiased=True)
5
6
7
duration = 5
8
animation = mpy.VideoClip(make_frame, duration=duration).resize(0.5)
9
# # Video generation takes 10 seconds, GIF generation takes 25s
10
# #animation.write_videofile("Rotated_sym_distri_static_Python.mp4", fps=20)
11
animation.write_gif("Rotated_sym_distri_static_Python.gif", fps=20)
12
mlab.close(fig1)

The view’s azimuth angle will update each frame. If we want to handle the data, we should use the “mlab.source” to update the arguement as is shown in the offical example

We finally export the gif file and I learned this from this Blog

The figure is a little big and we can compress this gif file in the online web

The following is the figure we obtain

The rotated 3d contour plots

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

Comment