我想将MATLAB中的surf命令转换为matplotlib中的plot_surface命令.
我正面临的挑战是在plot_surface命令中使用cmap函数来使用渐变来绘制曲面.
这是matlab脚本
% 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))
这是python scipt
当使用python和matplotlib创建一个类似的功能时,我无法用渐变色表面.
# Python-matplotlib Commands
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as 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()
我收到以下错误消息:
Traceback (most recent call last):
File "", line 1, in
File "C:\Python26\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 122, in runfile
execfile(filename, glbs)
File "C:\Documents and Settings\mramacha\My Documents\Python\Candela\tmp.py", line 13, in
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'
任何投入将是有帮助的.
Praboo