Skip to content

Commit 4876d10

Browse files
authored
Merge pull request #19 from PytLab/feature/pyvista-migration
Migrate 3D visualization from mayavi to PyVista
2 parents a48bb43 + 742145b commit 4876d10

7 files changed

Lines changed: 164 additions & 110 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ Thumbs.db
8282
# Jupyter notebook
8383
.ipynb_checkpoints/
8484

85+
# Claude Code
86+
.claude/
87+

README.rst

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,13 @@ Installation
4646

4747
python setup.py install
4848

49-
If you want to use **mayavi** to visualize VASP data, it is recommened to install `Canopy environment <https://store.enthought.com/downloads/#default>`_ on your device instead of installing it manually.
49+
3D visualization is done via **PyVista**, which is installed automatically as a dependency::
5050

51-
After installing canopy, you can set corresponding aliases, for example:
52-
53-
.. code-block:: shell
54-
55-
alias canopy='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/python'
56-
alias canopy-pip='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/pip'
57-
alias canopy-ipython='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/ipython'
58-
alias canopy-jupyter='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/jupyter'
51+
pip install vaspy
5952

60-
Then you can install VASPy to canopy::
53+
Or install PyVista separately::
6154

62-
canopy-pip install vaspy
55+
pip install pyvista
6356

6457
Examples
6558
--------
@@ -98,7 +91,7 @@ Visualize ELFCAR
9891
>>> from vaspy.electro import ElfCar
9992
>>> a = ElfCar()
10093
>>> a.plot_contour() # Plot coutour
101-
>>> a.plot_mcontour() # Plot coutour using mlab(with Mayavi installed)
94+
>>> a.plot_mcontour() # Plot coutour using PyVista
10295
>>> a.plot_contour3d() # Plot 3D coutour
10396
>>> a.plot_field() # Plot scalar field
10497

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
matplotlib>=1.5.2
22
numpy>=1.11.1
33
scipy>=0.18.0
4-
4+
pyvista>=0.42.0

scripts/md_viz/map_mayavi.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import numpy as np
66
from scipy.interpolate import interp2d
7-
from mayavi import mlab
7+
import pyvista as pv
88

99
from vaspy.iter import XdatCar, AniFile
1010
from traj import get_trajectories
@@ -43,11 +43,15 @@ def locate(x, y, position):
4343
newy = np.linspace(left, right, interp_resolution)
4444
newz = interp_func(newx, newy)
4545

46-
newy, newx = np.meshgrid(newx, newy)
47-
48-
face = mlab.surf(newx, newy, newz, warp_scale=40)
49-
mlab.axes(xlabel="x", ylabel="y", zlabel="z")
50-
mlab.outline(face)
51-
52-
mlab.show()
53-
46+
# PyVista surface plot
47+
nx, ny = len(newx), len(newy)
48+
X, Y = np.meshgrid(newx, newy, indexing='ij')
49+
Z = newz.T
50+
surface = pv.StructuredGrid(X[:, :, None], Y[:, :, None], Z[:, :, None])
51+
surface.point_data['scalars'] = Z[:, :, None].flatten(order='F')
52+
53+
pl = pv.Plotter()
54+
pl.add_mesh(surface, scalars='scalars', cmap='viridis',
55+
show_scalar_bar=True)
56+
pl.add_axes(xlabel='x', ylabel='y', zlabel='z')
57+
pl.show()

scripts/plot_test.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from plotter import *
22
import numpy as np
33
from scipy.interpolate import interp2d
4-
from mayavi import mlab
4+
import pyvista as pv
55
import mpl_toolkits.mplot3d
66
import matplotlib.pyplot as plt
77

@@ -17,20 +17,14 @@
1717
newy = np.linspace(0, np.max(y), 1000)
1818
newz = interpfunc(newx, newy)
1919

20-
#extent = [np.min(newx), np.max(newx), np.min(newy), np.max(newy)]
21-
#plt.contourf(newx.reshape(-1), newy.reshape(-1), newz, 20, extent=extent)
22-
#plt.colorbar()
23-
#
24-
##3d plot
25-
#fig3d = plt.figure()
26-
#ax3d = fig3d.add_subplot(111, projection='3d')
27-
#ax3d.plot_surface(newx, newy, newz, cmap=plt.cm.RdBu_r)
28-
#
29-
#plt.show()
20+
# PyVista surface plot
21+
nx, ny = len(newx), len(newy)
22+
X, Y = np.meshgrid(newx, newy, indexing='ij')
23+
Z = newz.T
24+
surface = pv.StructuredGrid(X[:, :, None], Y[:, :, None], Z[:, :, None])
25+
surface.point_data['scalars'] = Z[:, :, None].flatten(order='F')
3026

31-
#mlab
32-
face = mlab.surf(newx, newy, newz, warp_scale=2)
33-
mlab.axes(xlabel='x', ylabel='y', zlabel='z')
34-
mlab.outline(face)
35-
36-
mlab.show()
27+
pl = pv.Plotter()
28+
pl.add_mesh(surface, scalars='scalars', cmap='viridis', show_scalar_bar=True)
29+
pl.add_axes(xlabel='x', ylabel='y', zlabel='z')
30+
pl.show()

setup.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,21 @@
5858
5959
python setup.py install
6060
61-
If you want to use **mayavi** to visualize VASP data, it is recommened to install `Canopy environment <https://store.enthought.com/downloads/#default>`_ on your device instead of installing it manually.
61+
3D visualization is done via **PyVista**, which is installed automatically as a dependency::
6262
63-
After installing canopy, you can set corresponding aliases, for example:
64-
65-
.. code-block:: shell
66-
67-
alias canopy='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/python'
68-
alias canopy-pip='/Users/zjshao/Library/Enthought/Canopy/edm/envs/User/bin/pip'
69-
alias canopy-ipython='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/ipython'
70-
alias canopy-jupyter='/Users/<yourname>/Library/Enthought/Canopy/edm/envs/User/bin/jupyter'
63+
pip install vaspy
7164
72-
Then you can install VASPy to canopy::
65+
Or install PyVista separately::
7366
74-
canopy-pip install vaspy
67+
pip install pyvista
7568
7669
"""
7770

7871
install_requires = [
7972
'numpy>=1.11.1',
8073
'matplotlib>=1.5.2',
8174
'scipy>=0.18.0',
75+
'pyvista>=0.42.0',
8276
]
8377

8478
license = 'LICENSE'

0 commit comments

Comments
 (0)