I know there are programs that can automatically visualize .cube files (like VMD), but I'm trying to do it myself in python/matplotlib (or equivalent) to better understand what the numbers mean.
I understand that there are nx*ny*nz grid points in the file (and can be saved in an array fo dimensions [nx,ny,nz]), but I can't quite figure out how to visualize them or what kind of plot they correspond to (with respect to the modules available on matplotlib).
Part of a script that extracts info from cube file :
filename1 = 'density.cube'
with open(filename1, 'r') as f:
lines = f.read().splitlines()
no_of_atoms, _, _, _ = lines[2].split()
no_of_atoms = int(no_of_atoms)
xdim, _, _, _ = lines[3].split()
xdim = int(xdim)
ydim, _, _, _ = lines[4].split()
ydim = int(ydim)
zdim, _, _, _ = lines[5].split()
zdim = int(zdim)
elements = [None] * no_of_atoms
atoms_x_coords_in_density = [None] * no_of_atoms
atoms_y_coords_in_density = [None] * no_of_atoms
atoms_z_coords_in_density = [None] * no_of_atoms
for _ in range(no_of_atoms):
(elements[_], __,
atoms_x_coords_in_density[_],
atoms_y_coords_in_density[_],
atoms_z_coords_in_density[_]) = lines[6 + _].split()
elements[_] = int(elements[_])
atoms_x_coords_in_density[_] = float(atoms_x_coords_in_density[_])
atoms_y_coords_in_density[_] = float(atoms_y_coords_in_density[_])
atoms_z_coords_in_density[_] = float(atoms_z_coords_in_density[_])
# Load the data, we need to remove the first 8 lines and the space after
str = ' '.join(file(filename1).readlines()[(6 + no_of_atoms):])
data = np.fromstring(str, sep=' ')
data.shape = (xdim, ydim, zdim)
So, the grid points are in the data
array, but I'm not sure how to plot them. I've seen an example using the mayavi
package, but that's also a black box solution (plus I couldn't get it installed for some reason -- the conda version seems broken). I'm trying to understand about what's happening under the hood.
Any help is appreciated.