2017-09-16 125 views
0

情節,我剛開始使用pyqtgraph,我想使3D表面圖球面座標。我從文檔中查看了示例GLSurfacePlot.py,但只有笛卡爾座標中的圖。球面座標在pyqtgraph

這就是我想要做(這是一個半波偶極輻射模式)的情節:

enter image description here

如何繪製與pyqtgraph R(θ,Φ)?

編輯:我可以matplotlib mplot3d做到這一點,這裏是腳本:

import matplotlib.pyplot as plt 
import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 

k = 2*np.pi 
long = 0.5 
theta = np.linspace(0, np.pi, 361) 
phi = np.linspace(0, 2*np.pi, 361) 
PHI, THETA = np.meshgrid(phi, theta) 
R = np.absolute((np.cos(k*long/2*np.cos(THETA))-np.cos(k*long/2))/np.sin(THETA)) 
R = np.nan_to_num(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(111, projection = '3d') 
cmap = plt.get_cmap('jet') 
plot = ax.plot_surface(X, Y, Z, rstride=10, cstride=10, facecolors=cmap(R),linewidth=0, antialiased=False, alpha=1) 

plt.show() 

的問題是,它的旋轉和縮放它的時候的速度太慢了,我肯定需要這個功能對我的應用程序這就是爲什麼我試圖用pyqtgraph做到這一點。

+0

你可以放置數學方程 – eyllanesc

+0

你是什麼意思? – Jorge

+0

我的意思是什麼是方程'r(theta,phi)'而不使用代碼。 – eyllanesc

回答

0

繪製這種類型的方程是不可能通過GLSurfacePlotItem,在這種情況下,你必須使用GLMeshItem,但對於這一點,你必須創建一個合適的MeshData,因此它需要爲獲得下列函數的引用sphere

def DipoleData(rows, cols, func, args=None): 
    verts = np.empty((rows+1, cols, 3), dtype=float) 
    phi = (np.arange(rows+1) * 2*np.pi *(1+2/rows)/ rows).reshape(rows+1, 1) 
    th = ((np.arange(cols) * np.pi/cols).reshape(1, cols)) 

    if args is not None: 
     r = func(th, phi, *args) 
    else: 
     r = func(th, phi) 
    s = r* np.sin(th) 
    verts[...,2] = r * np.cos(th) 
    verts[...,0] = s * np.cos(phi) 
    verts[...,1] = s * np.sin(phi) 

    verts = verts.reshape((rows+1)*cols, 3)[cols-1:-(cols-1)] ## remove redundant vertexes from top and bottom 
    faces = np.empty((rows*cols*2, 3), dtype=np.uint) 
    rowtemplate1 = ((np.arange(cols).reshape(cols, 1) + np.array([[0, 1, 0]])) % cols) + np.array([[0, 0, cols]]) 
    rowtemplate2 = ((np.arange(cols).reshape(cols, 1) + np.array([[0, 1, 1]])) % cols) + np.array([[cols, 0, cols]]) 
    for row in range(rows): 
     start = row * cols * 2 
     faces[start:start+cols] = rowtemplate1 + row * cols 
     faces[start+cols:start+(cols*2)] = rowtemplate2 + row * cols 
    faces = faces[cols:-cols] ## cut off zero-area triangles at top and bottom 

    ## adjust for redundant vertexes that were removed from top and bottom 
    vmin = cols-1 
    faces[faces<vmin] = vmin 
    faces -= vmin 
    vmax = verts.shape[0]-1 
    faces[faces>vmax] = vmax 

    return gl.MeshData(vertexes=verts, faces=faces) 

app = QtGui.QApplication([]) 
w = gl.GLViewWidget() 
w.opts['distance'] = 3 
w.show() 
w.setWindowTitle('Half Wave Dipole Radiation Pattern') 

def r_theta_phi(theta, phi, k, l): 
    return np.absolute((np.cos((k*l/2)*np.cos(theta)) -np.cos(k*l/2))/np.sin(theta)) 

p = 2*np.pi 
q = 0.5 

md = DipoleData(100, 100, r_theta_phi, args=(p, q)) 
colors = np.ones((md.faceCount(), 4), dtype=float) 
colors[:,0] = np.linspace(0.1, 0.2, colors.shape[0]) 
colors[:,1] = np.linspace(0.2, 0.9, colors.shape[0]) 
colors[:,2] = np.linspace(0.0, 0.1, colors.shape[0]) 
md.setFaceColors(colors) 
m = gl.GLMeshItem(meshdata=md, smooth=False) 
w.addItem(m) 

ax = gl.GLAxisItem() 
ax.setSize(100,100,100) 
w.addItem(ax) 

g = gl.GLGridItem() 
g.scale(0.2, 0.2, 0.2) 
w.addItem(g) 

## Start Qt event loop unless running in interactive mode. 
if __name__ == '__main__': 
    import sys 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
     QtGui.QApplication.instance().exec_() 

獲得什麼是如下圖所示:

它然後在下面的實施例中使用

enter image description here

+0

嗯,這是從它應該是什麼樣子完全不同。 http://www.antenna-theory.com/antennas/norm3D1lam.jpg – Jorge

+0

你可以告訴我這本書,也許你通過我的等式是另一個,我是一個電子音樂學生,我已經在其他程序中模擬了它,而我得到類似的東西。 :P – eyllanesc

+0

天線理論:分析和設計。康斯坦丁A.巴蘭尼斯。 (第182-183頁)。在這裏你可以看看我製作的捕獲圖片: https://gyazo.com/467868b5353b8eb1d8fb7b35f6d4833b。我只關心歸一化的幅度,所以我正在括號內表達。這是一個比我通過你的表達式更簡單的表達式,因爲它已經取代了k和l值。我用matplotlib mplot3d繪製了它,得到了預期的圖形,但是它太慢了,這就是爲什麼我想使用pyqtgraph。 – Jorge