Python-绘制天线辐射图

时间:2019-02-22 08:26:28

标签: python matplotlib plot spherical-coordinate

这是我找到并稍作修改的代码。如何从原点缩放颜色并设置原点的坐标轴以进行可视化?我已经尝试寻找信息,但其中大多数是针对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()

试图获得与此近似的结果

2 个答案:

答案 0 :(得分:1)

您可以使用以下方法添加单位长度的轴线:

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的{​​{1}}参数在这里:

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)

这给了我这个结果:

enter image description here

如您所见,表面的颜色从原点附近的蓝色变成远离原点的红色。将此代码应用于您的数据应该并不困难。

答案 1 :(得分:1)

这是基于安德里亚(Andrea)的出色回答而来的,其中有几项补充对于可能在点之间有相当大间距的真实数据很有帮助。当我第一次绘制间距为45度的东西时,它看起来像这样:

Initial Plot

有两个明显的问题:

  1. 这些脸非常大,即使颜色范围很广,也只有一种颜色。
  2. 形状关于原点是对称的,但是脸部的颜色不是对称的。

可以通过对数据进行线性插值来改善问题1,以便将每个面孔分为多个可以具有不同颜色的部分。

由于分配了脸部颜色,发生了问题2。想象一下2D平面上的3x3点网格,每个点都有一个值。绘制表面时,将只有2x2个面,因此最后一行值的列将被丢弃,并且每个面的颜色仅由该面的一个角确定。我们真正想要的是每张面孔中心的价值。我们可以通过取四个角值的平均值并使用它来分配颜色来进行估算。

计算得出的结果与问题1的内插相似,因此我对两者都使用了相同的函数“ interp_array”。我不是一个Python程序员,所以可能是一种更有效的方法,但是它可以完成工作。

这是固定有问题2但没有插值的地块。对称是固定的,但仅使用两种颜色,因为这些面与原点的距离相等。

Symmetry Fix

这是点之间的8倍插值的最终图。现在,它更接近您在商用天线测量软件中会看到的那种连续色图了。

Interpolation Added

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()