使用颜色图在 mplot 3D 中表示 4D 数据 [英] Representing 4D data in mplot 3D using colormaps

查看:30
本文介绍了使用颜色图在 mplot 3D 中表示 4D 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to change the value that the colormap is tied to in an mplot3d surface plot?
As an example, I'm trying to represent surface temperature for an object:

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' 值吗?

This generates a 3d representation of the object, but the colormap is by default tied to the z-axis value. Can the colormap be tied to the 'temp' value?

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

(in this example, 'temp' maps on to Z the same way that the 'radius' values do)

我知道 MayaVI 之类的工具,但如果可能的话,我希望在 matplotlib 中找到解决方案.

I'm aware of tools like MayaVI, but if it's possible I'm hoping for a solution within matplotlib.

推荐答案

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

Try using facecolors in the call to plot_surface:

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()

这篇关于使用颜色图在 mplot 3D 中表示 4D 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆