在matplotlib中使用频谱图数据绘制PolyCollection 3d

时间:2019-02-20 16:21:32

标签: python mplot3d

我正在尝试从频谱图函数的输出数据中绘制一个多集合。

# TestData
test=np.random.rand(1000000)*100    
# Calc spectrogram
fa = 2e6  # Sampling Rate
fAx_signal, tAx_signal, STFTspec_signal = spsi.spectrogram(test, fa, 
nperseg=1024)
# get power for Frequency Bins (so i have the Frequency/Power for every Timestep)
freq_power_dict = {}
for k in range(0, len(tAx_signal)):
    power_list_t = []
    for i in range(0, len(fAx_signal)):
        power_list_t.append(STFTspec_signal[i][k])
    power_list_t_log = 20 * np.log10(power_list_t)
        freq_power_dict[k] = power_list_t_log

    # Polyplot from Matplotlib Docs for the "raw Data of the spectrogram function
import matplotlib.pyplot as plt
from matplotlib.collections import PolyCollection
from mpl_toolkits.mplot3d import axes3d
import numpy as np

# I think here is something wrong in my Code

freq_data = fAx_signal
amp_data = STFTspec_signal
time_data = tAx_signal

# this is from a user here at sof

verts = []
for itime in range(len(time_data)):
    # I'm adding a zero amplitude at the beginning and the end to get a nice
    # flat bottom on the polygons
    xs = np.concatenate([[freq_data[0, itime]], freq_data[:, itime], [freq_data[-1, itime]]])
    ys = np.concatenate([[0], amp_data[:, itime], [0]])
    verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts)
poly.set_alpha(0.7)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.add_collection3d(poly, zs=time_data, zdir='y')
ax.set_xlim3d(freq_data.min(), freq_data.max())
ax.set_xlabel('Frequency')
ax.set_ylim3d(time_data.min(), time_data.max())
ax.set_ylabel('Time')
ax.set_zlim3d(amp_data.min(), amp_data.max())
ax.set_zlabel('Amplitude')

plt.show()

我想做的是,查看3d多重绘图中每个时间步长的频率功率。这只是我想要的一个示例,我在工作中使用了一些类似的数据。

期待您的回答,并在此先感谢您。

最佳问候 巴斯蒂安

0 个答案:

没有答案