contourf() 在有限数据上绘制空白区域 [英] contourf() plots white space over finite data

查看:89
本文介绍了contourf() 在有限数据上绘制空白区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm attempting to plot a 3D chart using matplotlib.pyplot.contourf() with the following program:

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)

在大多数情况下,它运行良好;但是,在某些地方绘制了无用的空白区域.可以在此处找到(抱歉,我没有足够的声望点直接附加图像)

For the most part it is working fine; however, in some places useless white space is plotted. the plot produced can be found here (sorry, I don't have enough reputation points to attach images directly)

该程序的目的是计算一系列跨二维 numpy 数组的 1 个轴的快速傅立叶变换,并将它们堆叠起来以显示描绘数据中哪些周期性最突出的等高线图.

The purpose of this program is to calculate a series of Fast Fourier Transforms across 1 axis of a 2 dimensional numpy array, and stack them up to display a contour plot depicting which periodicities are most prominent in the data.

我检查了标绘数量中显示为白色的部分,并且仍然存在有限值,尽管比图中其他地方的显着数量要小得多.

I checked the parts of the plotted quantity that appear white, and finite values are still present, although much smaller than noticable quantities elsewhere in the plot:

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

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

Is the white space present because the values are so small compared to the rest of the plot? I'm not sure at all how to fix this.

推荐答案

您可以通过添加选项 extend='both' 来解决此问题.例如.C = plt.contourf(P,Altitude,Fourier,100,cmap='jet',extend='both')

You can fix this problem by adding option extend='both'. Eg. C = plt.contourf(P,Altitude,Fourier,100,cmap='jet',extend='both')

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

这篇关于contourf() 在有限数据上绘制空白区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆