我试图用plt.contour绘制地震波图。
我有三个数组:
这是我到目前为止的结果:

问题是,我想改变颜色条的比例:使一个等级,而没有这个白色时,振幅是低的。但我无法做到这一点,尽管我花了很多时间浏览文档。
我在这里读到plt.pcolormesh是不合适的(它只是在这里工作,因为我在一个特殊的情况下),但这是我想要得到的关于颜色和颜色栏:

这就是我写的代码:
T = len(time[0])*(time[0][1] - time[0][0]) # multiply ampFFT with T to offset
Z = abs(ampFFT)*(T) # abbreviation
# freq = frequency, ampFFT = Fast Fourier Transform of the amplitude of the wave
# freq, amFFT and time have same dimensions: 40 x 1418 (40 steps of time discretization x steps to have the total time. 2D because it is easier to use)
maxFreq = abs(freq).max() # maxium frequency for plot boundaries
maxAmpFFT = abs(Z).max()/2 # maxium ampFFT for plot boundaries of colorbar divided by 2 to scale better with the colors
minAmpFFT = abs(Z).min()
plt.figure(1)
plt.contour(time, freq, Z, vmin=minAmpFFT, vmax=maxAmpFFT)
plt.colorbar()
plt.ylim(0,maxFreq) # 0 to remove the negative frequencies useless here
plt.title("Amplitude intensity regarding to time and frequency")
plt.xlabel('time (in secondes)')
plt.ylabel('frequency (in Hz)')
plt.show()感谢您的关注!
注意:如果你想知道plt.pcolormesh:当我选择增加时间离散化的时候,情节是完全混乱的(这里我把时间平分到40,但是当我把时间平分到1000,情节是不正确的,我希望能够把时间分成更小的部分)。
编辑:当我使用plt.contourf而不是plt.contour时,我得到了这样的情节:

这也不是很有说服力。我理解为什么黄色占用这么大的空间(因为我设置了一个较低的vmax),但我不明白为什么我的情节中仍然有白色。
编辑2:我的老师绘制了我的数据,我有正确的数据。唯一的问题是左边的白色背景在我的情节(和深蓝色在左和右边框,也没有明显的原因,当我使用plt.contourf)。尽管有这些问题,最高的振幅在0.5赫兹左右,这与我的老师的工作是一致的。
他使用gnuplot,但由于我不知道gnuplot,所以我更喜欢使用python。
发布于 2018-05-24 08:43:05
解决方案/解决办法--我找到了
下面是我像countourf一样显示数据时所做的工作,但没有显示问题:
解释:表面上,我取的是abs(freq),而不是freq,因为我有负频率。
因为在计算FFT的频率时,有一个频率重复第二次,如下所示:

你有两种获得这个频率的方法:
fft.fftfreq 使用第二个选项, plot_surface不能很好地删除数组的数据(对我来说,它仍然是显示的)。所以我把频率值绝对化了,问题就消失了。
fig = plt.figure(1, figsize=(18,15)) # figsize: increase plot size
ax = fig.gca(projection='3d')
surf = ax.plot_surface(time, abs(freq), Z, rstride=1, cstride=1, cmap=cm.magma, linewidth=0, antialiased=False, vmin=minAmpFFT, vmax=maxAmpFFT)
ax.set_zlim(0, maxAmpFFT)
ax.set_ylim(0, maxFreq)
ax.view_init(azim=90, elev=90) # change view to top view, with axis in the right direction
plt.title("Amplitude intensity (m/Hz^0.5) regarding to time and frequency")
plt.xlabel('x : time (in secondes)')
plt.ylabel('y : frequency (in Hz)')
# ax.yaxis._set_scale('log') # should be in log, but does not work
plt.gca().invert_xaxis() # invert x axis !! MUST BE AFTER X,Y,Z LIM
plt.gca().invert_yaxis() # invert y axis !! MUST BE AFTER X,Y,Z LIM
plt.colorbar(surf)
fig.tight_layout()
plt.show()这就是我得到的情节:

https://stackoverflow.com/questions/50488327
复制相似问题