Python contourf()在有限数据上绘制空白
我正在尝试使用以下程序使用Python contourf()在有限数据上绘制空白,python,matplotlib,scipy,contourf,Python,Matplotlib,Scipy,Contourf,我正在尝试使用以下程序使用matplotlib.pyplot.contourf()绘制三维图表: import numpy as np import matplotlib.pyplot as plt import scipy # calculates Fast Fourier transforms for each value in the 1D array "Altitude" # and stacks them vertically to form a 2D array of fft va
matplotlib.pyplot.contourf()
绘制三维图表:
import numpy as np
import matplotlib.pyplot as plt
import scipy
# calculates Fast Fourier transforms for each value in the 1D array "Altitude"
# and stacks them vertically to form a 2D array of fft values called "Fourier"
Fourier = np.array([])
for i in range(len(Altitude)):
Ne_fft = Ne_lowpass[i,:]/np.average(Ne_lowpass[i,:])
Ne_fft = Ne_fft - Ne_fft.mean()
W = scipy.fftpack.fftfreq(10*Ne_fft.size, d=(Time[-1]-Time[0])/len(Ne_fft))
P = 1/abs(W)
FFT = abs(scipy.fftpack.fft(Ne_fft, n=10*len(Ne_fft)))
FFT = FFT**2
if len(Fourier) == 0:
Fourier = FFT
else:
Fourier = np.vstack((Fourier,FFT))
# plots the 2D contourf plot of "Fourier", with respect to "Altitude" and period "P"
plt.figure(5)
C = plt.contourf(P,Altitude,Fourier,100,cmap='jet')
plt.xscale('log')
plt.xlim([1,P[np.argmax(P)+1]])
plt.ylim([59,687])
plt.ylabel("Altitude")
plt.xlabel("Period")
plt.title("Power spectrum of Ne")
cbar = plt.colorbar(C)
cbar.set_label("Power", fontsize = 16)
在大多数情况下,它运行良好;但是,在某些地方,会绘制无用的空白。可以找到制作的情节(抱歉,我没有足够的声望点数直接附加图像)
该程序的目的是计算二维numpy阵列1轴上的一系列快速傅里叶变换,并将其叠加以显示等高线图,描绘数据中哪些周期最显著
我检查了显示为白色的打印数量部分,有限值仍然存在,尽管比打印中其他地方的可注意数量小得多:
print(Fourier[100:,14000:])
[[ 2.41147887e-03 1.50783490e-02 4.82620482e-02 ..., 1.49769976e+03
5.88859945e+02 1.31930217e+02]
[ 2.12684922e-03 1.44076962e-02 4.65881565e-02 ..., 1.54719976e+03
6.14086374e+02 1.38727145e+02]
[ 1.84414615e-03 1.38162140e-02 4.51940720e-02 ..., 1.56478339e+03
6.23619105e+02 1.41367042e+02]
...,
[ 3.51539440e-03 3.20182148e-03 2.38117665e-03 ..., 2.43824864e+03
1.18676851e+03 3.13067945e+02]
[ 3.51256439e-03 3.19924000e-03 2.37923875e-03 ..., 2.43805298e+03
1.18667139e+03 3.13042038e+02]
[ 3.50985146e-03 3.19677302e-03 2.37741084e-03 ..., 2.43790243e+03
1.18659640e+03 3.13021994e+02]]
print(np.isfinite(Fourier.all()))
True
print(np.isnan(Fourier.any()))
False
是否存在空白是因为与绘图的其余部分相比,这些值太小?我一点也不知道如何解决这个问题 在
plt.tourtf(p,高度,傅里叶,100,cmap='jet')行中
为等高线图自动选择100个标高。在这种情况下,“自动”并不保证这些级别包含所有数据
如果要确保包含所有数据,可以定义自己要使用的级别
plt.contourf(x, y, Z, np.linspace(Z.min(), Z.max(), 100))
您可以通过添加选项extend='both'来解决此问题。 例如C=plt.contourf(P,高度,傅里叶,100,cmap='jet',延伸='Tween')
参考:谢谢!工作得很有魅力。因此,问题是,空白区域中的数据太小,无法包含在轮廓级别中?我不知道问题的确切原因是什么,因为问题不包含可复制的示例。但我的猜测是,它们太接近一个有用的自动数字,例如,如果某些值是99.999,但最低级别被选择为100.0。我明白了。另外,我不得不收回我之前的评论。不管出于什么原因,它只在第一次运行时起作用,但在以后的运行中却没有任何变化。当然,你永远不能假设在没有测试的情况下就可以得到可接受的答案。