python - Python - 绘制天线辐射图
问题描述
这是我找到并稍作修改的代码。如何从原点缩放颜色并从原点设置轴以进行可视化?我试图找到信息,但其中大部分是用于二维图的。
在这里,我添加了两个以 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()
试图得到一个近似于此的结果
解决方案
您可以使用以下方法添加单位长度的轴线:
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')
关于表面的颜色,您需要定义一个表示与原点的距离的表达式,然后使用此表达式创建您的颜色图并将其传递给如下所示的facecolors
参数ax.plot_surface
:
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()
这给了我这个结果:
如您所见,表面的颜色从靠近原点的蓝色变为远离原点的红色。将此代码应用于您的数据应该不难。
推荐阅读
- php - 我应该如何在以下语句中单独打印 $headcount ?建议任何预定义的功能
- python - 写一个列表然后反转它返回
- android - 使用 recyclerview 布局图像
- .net-core - Service Fabric V2 远程处理自定义标头
- amazon-web-services - serverless.yml 如何在 Cognito Lambda Config 中引用 lambda 函数
- php - 找不到 Linux laravel 迁移驱动程序
- c# - 创建asp.net web api core 2.0项目的msi
- carousel - 如何使轮播中的非活动项目小于活动项目
- c - C 宏定义中的逗号 (,)
- c# - 从 C# 代码执行 python 脚本神秘地停止