Skip to main content
 首页 » 编程设计

python plot 3d彩色 map

2024年11月01日39unruledboy

我有 3 个一维数组,用于 X 值、Y 值和 Z 值。 我想用 X 和 Y 绘制一个二维图,并用 Z 表示颜色。

但是每次我尝试运行时我都会得到

AttributeError: 'list' object has no attribute 'shape' 

目前我有:

X=np.array(X) 
Y=np.array(Y) 
Z=np.array(Z) 
 
fig = pyplot.figure() 
ax = fig.add_subplot(111) 
p = ax.scatter(X,Y,Z) 

我也试过

fig, ax = pyplot.figure() 
p = ax.pcolor(X,Y,Z,cmap = cm.RdBu) 
cb = fig.colorbar(p,ax=ax) 

都给我同样的错误。

请您参考如下方法:

plt.scatter 的文档期望输入如下:

matplotlib.pyplot.scatter(x, y, s=20, ...) 

这不是 (x,y,z)。您将点的大小 s 设置为 Z 值。要给出颜色的“Z”值,将其作为 c 参数传递:

import numpy as np 
import pylab as plt 
 
# Example points that use a color proportional to the radial distance 
N = 2000 
X = np.random.normal(size=N) 
Y = np.random.normal(size=N) 
Z = (X**2+Y**2)**(1/2.0) 
 
plt.scatter(X,Y,c=Z,linewidths=.1) 
plt.axis('equal') 
plt.show()