简体   繁体   English

参数化3D曲面图,颜色取决于(x,y,z)

[英]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图(作为两个参数phitheta的函数)但我不知道如何使构成球体的多边形的颜色取决于参数thetaphi (通常,多边形的颜色简单地由多边形的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.

相关问题 当Z是Python中的列表列表时,如何用X,Y,Z绘制3D曲面? - How to plot 3D surface with X, Y, Z when Z is a list of list in Python? 如何在3D曲面图中绘制x,y,z的Numpy数组? - How can I plot a numpy array of x, y, z in 3D surface plot? 如何从 Plotly 中的 X、Y、Z 数组绘制 3D 表面? - How to plot a 3D surface from X,Y,Z array in Plotly? 3D表面图,其中z是采用由x和y形成的向量的函数 - 3D Surface Plot where z is a function that takes a vector formed from x and y 在python中从{x,y,z} - 散射数据绘制3D表面 - Plot a 3D surface from {x,y,z}-scatter data in python (python) 绘制 3d 表面,颜色图为第 4 维,x,y,z 的函数 - (python) plot 3d surface with colormap as 4th dimension, function of x,y,z 给定 x,y,z 坐标的数据,我将如何在 Matplotlib 中制作 3D 表面 plot? - How would I make a 3D surface plot in Matplotlib given this data of x,y,z coordinates? 使用 Matplotlib a*y + b*x + c 绘制 3D 曲面 - Plot 3D surface with Matplotlib a*y + b*x + c 如何制作 3D plot(X,Y,Z),将 Z 值分配给 X,Y 有序对? - How to make a 3D plot (X, Y, Z), assigning Z values to X,Y ordered pairs? Python-如何在表面离散3D点(x,y,z)之后从给定的x,y获取z值 - Python - How to get z value from given x, y after surface discrete 3D points (x,y,z)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM