【问题标题】:Python, Spherical Plot- Color ScalingPython,球形图 - 颜色缩放
【发布时间】:2019-07-21 01:50:34
【问题描述】:

我对 python 很陌生。在过去的两天里,我一直在试图弄清楚如何使用 matplotlib 缩放 3d 图(天线辐射图案)的颜色。看起来缩放在 xyz 轴之一中起作用,但在缩放从原点(半径)开始时不起作用。非常感谢任何帮助。

这不是我的代码,但我发现它非常有用。

这是代码:

  • 数值是从excel文档中读取的

  • 如您所见,我正在尝试使用此命令 colors=plt.cm.jet((R)/(Rmax)),但它不起作用。

              import pandas as pd
              import numpy as np
              import matplotlib.pyplot as plt
              import mpl_toolkits.mplot3d.axes3d as axes3d
    
              # Read data file and plot
              df = pd.read_csv('EIRP_Data.csv') #henter data fra Excel
    
              theta1d = df['Theta']                  
              theta1d = np.array(theta1d);
              theta2d = theta1d.reshape([37,73]) #"Theta" kolonen blir hentet ut, satt i numpy array og gjort om til 2d array
    
              phi1d = df['Phi']
              phi1d = np.array(phi1d);
              phi2d = phi1d.reshape([37,73]) #"Phi" kolonen blir hentet ut, satt i numpy array og gjort om til 2d Array
    
              power1d = df['Power']
              power1d = np.array(power1d);
              power2d = power1d.reshape([37,73]) #"Power" kolonen blir hentet ut, satt i numpy array og gjort om til 2d array
    
              THETA = np.deg2rad(theta2d)
              PHI = np.deg2rad(phi2d)
              R = power2d
              Rmax = np.max(R)
              Rmin = np.min(R)
              N = R / Rmax
    
              #Gjør om polar til kartesisk
              X = R * np.sin(THETA) * np.cos(PHI) 
              Y = R * np.sin(THETA) * np.sin(PHI)
              Z = R * np.cos(THETA)
    
              fig = plt.figure()
    
              #plot spesifikasjoner/settings
              ax = fig.add_subplot(1,1,1, projection='3d') 
              ax.grid(True)
              ax.axis('on')
              ax.set_xlabel('X')
              ax.set_ylabel('Y')
              ax.set_zlabel('Z')
              ax.set_xticklabels([]) 
              ax.set_yticklabels([])
              ax.set_zticklabels([])
    
              #colors =plt.cm.jet( (X.max()-X)/float((X-X.min()).max()))
              colors =plt.cm.jet( (R)/(Rmax) )
              ax.plot_surface(
                  X, Y, Z, rstride=1, cstride=1, facecolors=colors,
                  linewidth=0, antialiased=True, alpha=0.5, zorder = 0.5)
    
              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=20, cstride=20)
    
              plt.show()
    

【问题讨论】:

    标签: python matplotlib colors spherical-coordinate


    【解决方案1】:

    我有以下代码来考虑色标的半径。成功的方法是使用颜色图来获取标准化 R 的颜色值(这里是颜色权重)。

    X = np.ones((phiSize, thetaSize))                                                                           # Prepare arrays to hold the cartesian coordinate data.
    Y = np.ones((phiSize, thetaSize))
    Z = np.ones((phiSize, thetaSize))
    color_weight = np.ones((phiSize, thetaSize))
    
    min_dBi = np.abs(df["dBi"].min())
    
    for phi_idx, phi in enumerate(np.unique(df["Phi"])):
        for theta_idx, theta in enumerate(np.unique(df["Theta"])):
            e = df.query(f"Phi=={phi} and Theta=={theta}").iloc[0]["dBi"]
            e = min_dBi + e # so we dont have any negative numbers
            xe, ye, ze = sph2cart1(e, math.radians(theta), math.radians(phi))                                   # Calculate cartesian coordinates
    
            X[phi_idx, theta_idx] = xe                                                                                  # Store cartesian coordinates
            Y[phi_idx, theta_idx] = ye
            Z[phi_idx, theta_idx] = ze
            color_weight[phi_idx, theta_idx] = e
    
    ax.plot_surface(X, Y, Z, color='b')                                                                         # Plot surface
    plt.ylabel('Y')
    plt.xlabel('X')                                                                                             # Plot formatting
    plt.show()
    

    phisizethetaSize 是我数据中唯一 phi 和 theta 的数量。 我的天线的 dBi 存储在 dBi 列中的 pandas 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-07
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2018-11-17
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多