gpt4 book ai didi

python - contourf() 在有限数据上绘制空白区域

转载 作者:行者123 更新时间:2023-12-01 01:45:34 27 4
gpt4 key购买 nike

我正在尝试通过以下程序使用 matplotlib.pyplot.contourf() 绘制 3D 图表:

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)

在大多数情况下,它运行良好;然而,在某些地方绘制了无用的空白。生成的图可以找到 here (抱歉,我没有足够的声望点,无法直接附加图片)

该程序的目的是计算二维 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

是否存在空白,因为这些值与绘图的其余部分相比太小?我完全不知道如何解决这个问题。

最佳答案

您可以通过添加选项 extend='both' 来解决此问题。

示例:

C = plt.contourf(P,Altitude, Fourier,100, cmap='jet', extend='both')

引用号:https://matplotlib.org/examples/pylab_examples/contourf_demo.html

关于python - contourf() 在有限数据上绘制空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51386947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com