I am stuck in a problem with matplotlib and using numpy.meshgrid. I have a 3D mesh of points called "grid3" in format x, y, z, pot. The point mesh is organized in the following way:
x = grid3[:,0]
y = grid3[:,1]
z = grid3[:,2]
pot = grid3[:,3]
My mesh file looks like this:
# Format: x,y,z,pot
53.500 2.000 34.500 1.152
53.500 2.000 35.000 0.932
53.500 2.000 35.500 0.932
53.500 2.000 36.000 1.222
53.500 2.000 36.500 0.739
53.500 2.000 37.000 1.152
53.500 2.000 37.500 0.809
53.500 2.000 38.000 0.647
53.500 2.000 38.500 1.393
53.500 2.000 39.000 1.635
53.500 2.000 39.500 1.635
53.500 2.000 40.000 1.635
My intention is to obtain a 2D graph of the potential along a section of x = val and that this graph is "smoothed" by contour KDE type or isocontorno lines.
import matplotlib as plt
import numpy as np
yy = grid3[grid3[:,0] == val][:,1]
zz = grid3[grid3[:,0] == val][:,2]
pot = grid3[grid3[:,0] == val][:,3]
X, Y = np.meshgrid(yy.reshape(41,41), zz.reshape(41,41),sparse=True)
print "yy = ",yy.shape
print "zz = ",zz.shape
Z = pot.reshape(41,41)
plt.contourf(X, Y, Z, 20, cmap='RdGy')
plt.show()
However, I always get a dimensional error and I am exasperated:
Traceback (most recent call last):
File "./plot_grid2.py", line 496, in <module>
plot_grid(grid3)
File "./plot_grid2.py", line 418, in plot_grid
plt.contourf(X, Y, Z, 20, cmap='RdGy')
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2818, in contourf
ret = ax.contourf(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1717, in inner
return func(ax, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 5827, in contourf
contours = mcontour.QuadContourSet(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/contour.py", line 853, in __init__
kwargs = self._process_args(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/contour.py", line 1447, in _process_args
x, y, z = self._contour_args(args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/contour.py", line 1528, in _contour_args
x, y, z = self._check_xyz(args[:3], kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/contour.py", line 1588, in _check_xyz
"{0} instead of {1}.".format(x.shape, z.shape))
TypeError: Shape of x does not match that of z: found (1, 1681) instead of (41, 41).
The dimensions of the numpy arrays are:
yy.shape vale: (1681,)
zz.shape vale: (1681,)
I do not know what else to look ... can you guide me? Thank you very much