[英]How to plot a 3d surface from multiple 2d arrays and one 1d array?
[英]Create a 2D array (3D plot) from two 1D arrays (2D plots) in Python (for calculating the Hilbert spectrum)
我想在Python中將希爾伯特譜計算為3D圖(即2D陣列)。 希爾伯特譜是形式time x frequency -> amplitude
的函數,其為每個時間和頻率對分配幅度值。
用於計算頻譜的方法將一個或多個2D信號作為輸入,每個信號具有兩個分量: time -> frequency
和time -> amplitude
。 以單個信號a
為例。 y1
是頻率值, y2
是幅度值。
a_x = [1,2,3]
a_y1 = [1,2,1]
a_y2 = [4,5,6]
我想將這兩個2D圖轉換成一個3D圖,例如X x Y1 -> Y2
。
a(1,1) = 4
a(2,2) = 5
a(3,1) = 6
現實生活中的價值觀將是花車。 到目前為止,我的解決方案是在y1
取最大值和最小值,並以預定的精度(例如0.01)初始化網格。 在這個例子中:
y1_max = np.amax(a_y1)
y1_min = np.amin(a_y2)
# Initialise 2d array of zeros
hilbert_spectrum = np.zeros((len(a_x), len(np.linspace(y1_min, y1_max, 0.01)))
然后我會這樣填寫網格:
# Fit the old y1 values into new grid
y1_grid = np.floor((a_y1 - y1_min) / 0.01).astype(np.int)
# Fill the 2D hilbert spectrum
hilbert_spectrum[1, y1_grid[0]] = 4
hilbert_spectrum[2, y1_grid[1]] = 5
hilbert_spectrum[3, y1_grid[2]] = 6
然而,當存在多個輸入信號時,這變得復雜。 有沒有更多數學/簡潔的方法來做到這一點? 輸出應該是2D數組,可用於進一步計算。
以下: http : //matplotlib.org/examples/mplot3d/trisurf3d_demo2.html
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
a_x = [1,2,3]
a_y1 = [1,2,1]
a_y2 = [4,5,6]
# Triangulate parameter space to determine the triangles
tri = mtri.Triangulation(a_x, a_y1)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
# The triangles in parameter space determine which x, y, z points are
# connected by an edge
ax.plot_trisurf(a_x, a_y1, a_y2, triangles=tri.triangles, cmap=plt.cm.Spectral)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.