3

有没有办法改变颜色图在 mplot3d 曲面图中绑定的值?
例如,我试图表示一个物体的表面温度:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

z = np.array([0,1,2,3,4,5,6,7,8,9,10])
radius = np.array([0,1,1.5,1,0,2,4,5,4,2,1])
temp = np.array([150,200,210,220,225,220,195,185,160,150,140])

angle = np.linspace(0,2*np.pi,20)
Z,ANG = np.meshgrid(z,angle)
# transform them to cartesian system
X,Y = radius*np.cos(ANG),radius*np.sin(ANG)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='jet')
plt.show()

这会生成对象的 3d 表示,但默认情况下,颜色图与 z 轴值相关联。颜色图可以绑定到“温度”值吗?

(在本例中,“temp”映射到 Z 的方式与“radius”值的映射方式相同)

我知道像 MayaVI 这样的工具,但如果可能的话,我希望在 matplotlib 中找到一个解决方案。

4

2 回答 2

3

尝试在对plot_surface的调用中使用facecolors

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

z = np.array([0,1,2,3,4,5,6,7,8,9,10])
radius = np.array([0,1,1.5,1,0,2,4,5,4,2,1])
temp = np.array([150,200,210,220,225,220,195,185,160,150,140])

angle = np.linspace(0,2*np.pi,20)
Z,ANG = np.meshgrid(z,angle)
T,ANG = np.meshgrid(temp,angle)
# transform them to cartesian system
X,Y = radius*np.cos(ANG),radius*np.sin(ANG)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=cm.jet(T/float(T.max())))
plt.show()

在此处输入图像描述

于 2013-02-09T19:26:14.283 回答
1

如果您也在寻找颜色条,请执行以下操作

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
p_surf=ax.plot_surface(x,y,z,rstride=1,cstride=1,linewidth=0,antialiased=True,facecolors=cm.jet(np.sqrt(x*x + y*y + z*z)))
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(x*x + y*y + z*z)
plt.colorbar(m)
plt.show()
于 2014-12-01T13:46:17.467 回答