Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

python - Representing 4D data in mplot 3D using colormaps

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

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?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...