【问题标题】:contourf() plots white space over finite datacontourf() 在有限数据上绘制空白
【发布时间】:2018-07-17 17:11:31
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

    标签: python matplotlib scipy contourf


    【解决方案1】:

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

    例子:

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

    参考:https://matplotlib.org/examples/pylab_examples/contourf_demo.html

    【讨论】:

      【解决方案2】:

      plt.contourf(P,Altitude,Fourier,100,cmap='jet') 行中,您为等高线图自动选择了 100 个级别。在这种情况下,“自动”不保证这些级别包括所有数据。

      如果你想确保他们的所有数据都包含在内,你可以定义你自己的级别来使用

      plt.contourf(x, y, Z, np.linspace(Z.min(), Z.max(), 100))
      

      【讨论】:

      • 谢谢!像魅力一样工作。那么问题是存在于空白区域的数据太小而无法包含在等高线级别中?
      • 我不确定问题是什么,因为该问题不包含可重现的示例。但我的猜测是它们太接近有用的自动数字,例如如果某些值为 99.999,但最低级别选择为 100.0。
      • 我明白了。另外,我必须撤回我之前的评论。无论出于何种原因,它只在第一次运行时起作用,但在以后的运行中没有任何改变。
      • 当然,如果没有minimal reproducible example,你永远不能假设得到可接受的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      相关资源
      最近更新 更多