(python) 用颜色图作为第 4 维绘制 3d 表面,x、y、z 的函数 [英] (python) plot 3d surface with colormap as 4th dimension, function of x,y,z
问题描述
我正在尝试绘制一个 3d 曲面,其中三个维度中的每一个都位于一个单独的值数组中,并且每个坐标处的曲面着色是 x、y、z 的函数.一种 numpy.pcolormesh 但在 4D 中,而不是 3D.3D 绘图由下式给出:
from mpl_toolkits.mplot3d 导入 Axes3D从 matplotlib 导入 cmfig = plt.figure()ax = fig.gca(projection='3d')x = np.logspace(-1.,np.log10(5),50)y = np.linspace(6,9,50)z = np.linspace(-1,1,50)颜色 = LikeBeta(y,range(50),range(50))ax.plot_trisurf(x,y,z,cmap=colors,linewidth=0.2)
哪里
def LikeBeta(rho0,r0,beta):M0 = 10**rho0*r0_array[r0]**3I = cst*M0*sigma_los_beta[beta,:,r0]S = dv**2+Ires = (np.log(S) + (v-u)**2/S).sum()返回 res/2.
可能 cmap=colors
是错误的,但问题出在别处.我收到以下错误:
---->8 种颜色 = LikeBeta(y,range(50),range(50))---->4 I = cst*M0*sigma_los_beta[beta,:,r0]ValueError:操作数无法与形状一起广播 (50,) (50,353)
确实 sigma_los_beta
是我单独评估的数组,形状为 (50,353,50)
,而那些 353 是我必须拥有的数据.
如何将此函数转换为与 plot_trisurf
的其他条目兼容的形式?
抱歉,我无法提供最少的工作代码,因为 dv、v 和 u 是数据.非常感谢您的帮助.干杯
我引用(和其他人)的答案提到您应该规范化您的第四维数据.似乎可以通过像我在代码示例中那样显式设置颜色图的限制来避免这种情况.
I'm trying to plot a 3d surface where each of the three dimensions in a separate array of values and the colouring of the surface at each coordinate is a function of x,y,z. A sort of numpy.pcolormesh but in 4D, rather than 3D. The 3D plot is given by:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.logspace(-1.,np.log10(5),50)
y = np.linspace(6,9,50)
z = np.linspace(-1,1,50)
colors = LikeBeta(y,range(50),range(50))
ax.plot_trisurf(x,y,z,cmap=colors,linewidth=0.2)
where
def LikeBeta(rho0,r0,beta):
M0 = 10**rho0*r0_array[r0]**3
I = cst*M0*sigma_los_beta[beta,:,r0]
S = dv**2+I
res = (np.log(S) + (v-u)**2/S).sum()
return res/2.
Probably the cmap=colors
is wrong, but the problem lies elsewhere. I get the following error:
----> 8 colors = LikeBeta(y,range(50),range(50))
----> 4 I = cst*M0*sigma_los_beta[beta,:,r0]
ValueError: operands could not be broadcast together with shapes (50,) (50,353)
Indeed sigma_los_beta
is an array that I evaluate separately and has shape (50,353,50)
and those 353 are data that I must have.
How can I cast this function into a form that is compatible with the other entries of plot_trisurf
?
Sorry, but I can't supply a minimal working code, because dv,v and u are data. Thank you very much for your help. Cheers
This answer addresses the 4d surface plot problem. It uses matplotlib's plot_surface
function instead of plot_trisurf
.
Basically you want to reshape your x, y and z variables into 2d arrays of the same dimension. To add the fourth dimension as a colormap, you must supply another 2d array of the same dimension as your axes variables.
Below is example code for a 3d plot with the colormap corresponding to the x values. The facecolors
argument is used to alter the colormap to your liking. Note that its value is acquired from the to_rgba()
function in the matplotlib.cm.ScalarMappable
class.
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# domains
x = np.logspace(-1.,np.log10(5),50) # [0.1, 5]
y = np.linspace(6,9,50) # [6, 9]
z = np.linspace(-1,1,50) # [-1, 1]
# convert to 2d matrices
Z = np.outer(z.T, z) # 50x50
X, Y = np.meshgrid(x, y) # 50x50
# fourth dimention - colormap
# create colormap according to x-value (can use any 50x50 array)
color_dimension = X # change to desired fourth dimension
minn, maxx = color_dimension.min(), color_dimension.max()
norm = matplotlib.colors.Normalize(minn, maxx)
m = plt.cm.ScalarMappable(norm=norm, cmap='jet')
m.set_array([])
fcolors = m.to_rgba(color_dimension)
# plot
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
fig.canvas.show()
The answer I referenced (and others) mentions that you should normalize your fourth dimension data. It seems that this may be avoided by explicitly setting the limits of the colormap as I did in the code sample.
这篇关于(python) 用颜色图作为第 4 维绘制 3d 表面,x、y、z 的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!