这是我找到并略微修改过的代码。如何从原点缩放颜色并从原点设置轴以进行可视化?我试着寻找信息,但大部分都是2d图。
在这里,我添加了两个间隔为45度的theta和phi数组,以及一个表示信号功率的随机数组。该图可以工作,但信号和间隔不太正确。我的目标是从原点添加坐标轴,并从原点缩放颜色。
import pandas as pd
import numpy as np
import scipy as sci
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as Axes3D
from matplotlib import cm, colors
from array import *
import random
#theta
vals_theta = array('i',[0,0,0,0,0,0,0,0,0,45,45,45,45,45,45,45,45,45,90,90,90,
90,90,90,90,90,90,135,135,135,135,135,135,135,135,135,
180,180,180,180,180,180,180,180,180])
#phi
vals_phi = array('i',[0,45,90,135,180,225,270,315,360,
0,45,90,135,180,225,270,315,360,
0,45,90,135,180,225,270,315,360,
0,45,90,135,180,225,270,315,360,
0,45,90,135,180,225,270,315,360])
#random numbers simulating the power data
vals_power = np.random.uniform(low=-7.2E-21, high=7.2E-21, size=(45,))
theta1d = vals_theta
theta1d = np.array(theta1d);
theta2d = theta1d.reshape([5,9])
phi1d = vals_phi
phi1d = np.array(phi1d);
phi2d = phi1d.reshape([5,9])
power1d = vals_power
power1d = np.array(power1d);
power2d = power1d.reshape([5,9])
THETA = np.deg2rad(theta2d)
PHI = np.deg2rad(phi2d)
R = power2d
Rmax = np.max(R)
X = R * np.sin(THETA) * np.cos(PHI)
Y = R * np.sin(THETA) * np.sin(PHI)
Z = R * np.cos(THETA)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.grid(True)
ax.axis('on')
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
N = R / Rmax
ax.plot_surface(
X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('jet'),
linewidth=0, antialiased=False, alpha=0.5, zorder = 0.5)
ax.set_title('Spherical 3D Plot', fontsize=20)
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(R)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(R)
fig.colorbar(m, shrink=0.8);
ax.view_init(azim=300, elev = 30)
# Add Spherical Grid
phi ,theta = np.linspace(0, 2 * np.pi, 40), np.linspace(0, np.pi, 40)
PHI, THETA = np.meshgrid(phi,theta)
R = Rmax
X = R * np.sin(THETA) * np.cos(PHI)
Y = R * np.sin(THETA) * np.sin(PHI)
Z = R * np.cos(THETA)
ax.plot_wireframe(X, Y, Z, linewidth=0.5, rstride=3, cstride=3)
print(theta1d)
print(theta2d)
print(power2d)
plt.show()尝试得到与此近似的结果

发布于 2020-07-20 20:01:10
可以使用以下命令添加统一长度的轴线:
ax.plot([0, 1], [0, 0], [0, 0], linewidth=2, color = 'red')
ax.plot([0, 0], [0, 1], [0, 0], linewidth=2, color = 'green')
ax.plot([0, 0], [0, 0], [0, 1], linewidth=2, color = 'blue')关于曲面的颜色,您需要定义一个表示到原点的距离的表达式,然后使用此表达式创建色彩映射并将其传递给ax.plot_surface的facecolors参数,如下所示:
dist = np.sqrt(X**2 + Y**2 + Z**2)
dist_max = np.max(dist)
my_col = cm.jet(dist/dist_max)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=my_col, linewidth=0, antialiased=False)完整的代码:
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = 8*np.sin(R)
dist = np.sqrt(X**2 + Y**2 + Z**2)
dist_max = np.max(dist)
my_col = cm.jet(dist/dist_max)
axes_length = 1.5
ax.plot([0, axes_length*dist_max], [0, 0], [0, 0], linewidth=2, color = 'red')
ax.plot([0, 0], [0, axes_length*dist_max], [0, 0], linewidth=2, color = 'green')
ax.plot([0, 0], [0, 0], [0, axes_length*dist_max], linewidth=2, color = 'blue')
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=my_col,
linewidth=0, antialiased=False)
ax.set_xlim([-axes_length*dist_max, axes_length*dist_max])
ax.set_ylim([-axes_length*dist_max, axes_length*dist_max])
ax.set_zlim([-axes_length*dist_max, axes_length*dist_max])
plt.show()这给了我这个结果:

如您所见,曲面的颜色从靠近原点的蓝色变为远离原点的红色。将此代码应用于您的数据应该不难。
发布于 2020-07-24 00:51:00
这是在Andrea的优秀答案的基础上构建的,增加了几个应该对实际数据有帮助的东西,这些数据在点之间可能有相当大的间距。当我第一次绘制间距为45度的东西时,它看起来是这样的:

有两个显而易见的问题:
问题1可以通过对数据进行线性插值来改进,以便将每个面部划分为可以具有不同颜色的多个部分。
问题2的发生是因为脸部颜色的分配方式。想象一下,在2D平面上有一个3x3的点网格,每个点都有一个值。绘制曲面时,将只有2x2个面,因此最后一行和最后一列的值将被丢弃,并且每个面的颜色仅由面的一个角确定。我们真正想要的是每个面中心的值。我们可以通过取四个角点值的平均值并使用它来分配颜色来估计这一点。
在计算上,这最终类似于问题1的插值,因此我对这两个问题使用了相同的函数"interp_array“。我不是一个Python程序员,所以可能有一种更有效的方法来做这件事,但它可以完成工作。
这是修复了问题2但没有插值的图。对称是固定的,但只使用了两种颜色,因为面与原点的间距相等。

这是最终的曲线图,两点之间有8x插值。现在,它更接近于在商业天线测量软件中看到的那种连续颜色图。

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as Axes3D
from matplotlib import cm, colors
def interp_array(N1): # add interpolated rows and columns to array
N2 = np.empty([int(N1.shape[0]), int(2*N1.shape[1] - 1)]) # insert interpolated columns
N2[:, 0] = N1[:, 0] # original column
for k in range(N1.shape[1] - 1): # loop through columns
N2[:, 2*k+1] = np.mean(N1[:, [k, k + 1]], axis=1) # interpolated column
N2[:, 2*k+2] = N1[:, k+1] # original column
N3 = np.empty([int(2*N2.shape[0]-1), int(N2.shape[1])]) # insert interpolated columns
N3[0] = N2[0] # original row
for k in range(N2.shape[0] - 1): # loop through rows
N3[2*k+1] = np.mean(N2[[k, k + 1]], axis=0) # interpolated row
N3[2*k+2] = N2[k+1] # original row
return N3
vals_theta = np.arange(0,181,45)
vals_phi = np.arange(0,361,45)
vals_phi, vals_theta = np.meshgrid(vals_phi, vals_theta)
THETA = np.deg2rad(vals_theta)
PHI = np.deg2rad(vals_phi)
# simulate the power data
R = abs(np.cos(PHI)*np.sin(THETA)) # 2 lobes (front and back)
interp_factor = 3 # 0 = no interpolation, 1 = 2x the points, 2 = 4x the points, 3 = 8x, etc
X = R * np.sin(THETA) * np.cos(PHI)
Y = R * np.sin(THETA) * np.sin(PHI)
Z = R * np.cos(THETA)
for counter in range(interp_factor): # Interpolate between points to increase number of faces
X = interp_array(X)
Y = interp_array(Y)
Z = interp_array(Z)
fig = plt.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
ax.grid(True)
ax.axis('on')
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
N = np.sqrt(X**2 + Y**2 + Z**2)
Rmax = np.max(N)
N = N/Rmax
axes_length = 1.5
ax.plot([0, axes_length*Rmax], [0, 0], [0, 0], linewidth=2, color='red')
ax.plot([0, 0], [0, axes_length*Rmax], [0, 0], linewidth=2, color='green')
ax.plot([0, 0], [0, 0], [0, axes_length*Rmax], linewidth=2, color='blue')
# Find middle points between values for face colours
N = interp_array(N)[1::2,1::2]
mycol = cm.jet(N)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=mycol, linewidth=0.5, antialiased=True, shade=False) # , alpha=0.5, zorder = 0.5)
ax.set_xlim([-axes_length*Rmax, axes_length*Rmax])
ax.set_ylim([-axes_length*Rmax, axes_length*Rmax])
ax.set_zlim([-axes_length*Rmax, axes_length*Rmax])
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(R)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.colorbar(m, shrink=0.8)
ax.view_init(azim=300, elev=30)
plt.show()https://stackoverflow.com/questions/54822873
复制相似问题