python - Color matplotlib plot_surface command with surface gradient -
i convert surf command matlab plot_surface command in matplotlib.
the challenge facing when using cmap function in plot_surface command color surface gradient.
here matlab script
% matlab commands x = -5:.25:5; y = x [x,y] = meshgrid(x); r = sqrt(x.^2 + y.^2); z = sin(r) surf(x,y,z,gradient(z))
the figure such command can found here. (http://www.mathworks.com/help/techdoc/visualize/f0-18164.html#f0-46458)
here python scipt when using python , matplotlib create similar function unable color surface gradient.
# python-matplotlib commands mpl_toolkits.mplot3d import axes3d matplotlib import cm import matplotlib.pyplot plt import numpy np fig = plt.figure() ax = fig.gca(projection='3d') x = np.arange(-5, 5, 0.25) y = np.arange(-5, 5, 0.25) x, y = np.meshgrid(x, y) r = np.sqrt(x**2 + y**2) z = np.sin(r) surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=gradient(z), linewidth=0, antialiased=false) plt.show()
i following error message:
traceback (most recent call last): file "<ipython console>", line 1, in <module> file "c:\python26\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 122, in runfile execfile(filename, glbs) file "c:\documents , settings\mramacha\my documents\python\candela\tmp.py", line 13, in <module> surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=gradient(z), linewidth=0, antialiased=false) file "c:\python26\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 729, in plot_surface polyc = art3d.poly3dcollection(polys, *args, **kwargs) file "c:\python26\lib\site-packages\mpl_toolkits\mplot3d\art3d.py", line 344, in __init__ polycollection.__init__(self, verts, *args, **kwargs) file "c:\python26\lib\site-packages\matplotlib\collections.py", line 570, in __init__ collection.__init__(self,**kwargs) file "c:\python26\lib\site-packages\matplotlib\collections.py", line 86, in __init__ cm.scalarmappable.__init__(self, norm, cmap) file "c:\python26\lib\site-packages\matplotlib\cm.py", line 155, in __init__ self.cmap = get_cmap(cmap) file "c:\python26\lib\site-packages\matplotlib\cm.py", line 126, in get_cmap if name in cmap_d: typeerror: unhashable type: 'list'
any inputs helpful.
praboo
first, looks want colors mapped gradient magnitude. trying use gradient vectors why getting 'list' error.
second, can supply cmap, defines how want z values mapped color. if want new face colors use facecolors
argument.
third, you'll want normalize values 0..1 map them thru colormap. (i think there way, dividing magnitude max pretty simple)
here's code:
# python-matplotlib commands mpl_toolkits.mplot3d import axes3d matplotlib import cm import matplotlib.pyplot plt import numpy np fig = plt.figure() ax = fig.gca(projection='3d') x = np.arange(-5, 5, .25) y = np.arange(-5, 5, .25) x, y = np.meshgrid(x, y) r = np.sqrt(x**2 + y**2) z = np.sin(r) gx, gy = np.gradient(z) # gradients respect x , y g = (gx**2+gy**2)**.5 # gradient magnitude n = g/g.max() # normalize 0..1 surf = ax.plot_surface( x, y, z, rstride=1, cstride=1, facecolors=cm.jet(n), linewidth=0, antialiased=false, shade=false) plt.show()
and result:
Comments
Post a Comment