[英]Parametric 3D Surface Plot with color depending on (x,y,z)
I want to plot a quantity which is given on a parametric surface in 3d space (for example the temperature distribution on a sphere). 我想绘制在3d空间中参数曲面上给出的数量(例如球体上的温度分布)。 I can plot a parametric 3D plot of the sphere (as a function of the two parameters phi
and theta
) but I don't know how to make the colors of the polygons making up the sphere depend on the parameters theta
and phi
(normally, the color of a polygon is simply determined by the z-Position of the polygon). 我可以绘制球体的参数化3D图(作为两个参数phi
和theta
的函数)但我不知道如何使构成球体的多边形的颜色取决于参数theta
和phi
(通常,多边形的颜色简单地由多边形的z位置确定。
Here's a basic example which plots a torus with colormap: 这是一个用彩色图绘制圆环的基本示例:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
angle = np.linspace(0, 2 * np.pi, 32)
theta, phi = np.meshgrid(angle, angle)
r, R = .25, 1.
X = (R + r * np.cos(phi)) * np.cos(theta)
Y = (R + r * np.cos(phi)) * np.sin(theta)
Z = r * np.sin(phi)
# Display the mesh
fig = plt.figure()
ax = fig.gca(projection = '3d')
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1,cmap="hot")
plt.show()
However, the colors of the files are given by the z position of the tile, I want the color to be given by a function f(x,y)
. 但是,文件的颜色由图块的z位置给出,我希望颜色由函数f(x,y)
。
Does anyone know how I can achieve this dependency in Matplotlib? 有谁知道我如何在Matplotlib中实现这种依赖?
Thanks very much! 非常感谢!
Ok, if anyone else is looking for a solution to this problem here's a possible solution: 好的,如果其他人正在寻找这个问题的解决方案,这里有一个可能的解决方案:
The colors of the individual faces making up the surface plot can be set using the keyword argument facecolors
. 可以使用关键字参数facecolors
设置构成曲面图的各个面的颜色。 The following code will use the function X**2+Y**2
for coloring the faces of the parametric surface: 以下代码将使用函数X**2+Y**2
来着色参数曲面的面:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# Generate torus mesh
angle = np.linspace(0, 2 * np.pi, 32)
theta, phi = np.meshgrid(angle, angle)
r, R = .25, 1.
X = (R + r * np.cos(phi)) * np.cos(theta)
Y = (R + r * np.cos(phi)) * np.sin(theta)
Z = r * np.sin(phi)
colorfunction=(X**2+Y**2)
norm=colors.Normalize(colorfunction.min(),colorfunction.max())
# Display the mesh
fig = plt.figure()
ax = fig.gca(projection = '3d')
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(-1, 1)
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, facecolors=cm.jet(norm(colorfunction)))
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.