Python Matplotlib:contourf在使用X,Y时重新缩放和轴错误
要求的完整测试代码:Python Matplotlib:contourf在使用X,Y时重新缩放和轴错误,python,matplotlib,plot,contourf,Python,Matplotlib,Plot,Contourf,要求的完整测试代码: import numpy as np import matplotlib.pyplot as plt # Numtopad determines number of padding points numtopad = 512 # Define axis x = np.arange(numtopad) y = x[:,np.newaxis] # Offsets which are zero x0 = 256*0 y0 = 256*0 # Exponentially
import numpy as np
import matplotlib.pyplot as plt
# Numtopad determines number of padding points
numtopad = 512
# Define axis
x = np.arange(numtopad)
y = x[:,np.newaxis]
# Offsets which are zero
x0 = 256*0
y0 = 256*0
# Exponentially decaying function in 2D
f = np.exp( -((y-y0) + (x-x0))/(10))
# Fourier transform above function and move zero frqeuencies to center of graph
f2 = np.fft.fft(f,axis=0)
f2 = np.fft.fft(f2,axis=1)
f2 = np.fft.fftshift(f2,axes=0)
f2 = np.fft.fftshift(f2,axes=1)
Delta_t = x[1]-x[0]
# Define a frequency
freq_t = np.fft.fftfreq(numtopad,d = Delta_t)
freq_offset = 200
E1 = freq_t + freq_offset
E2 = freq_t + freq_offset
# plt.contourf(abs(f2))
plt.contourf(E1,E2,abs(f2))
你能给出你的完整代码吗?因为图片不可用,请确保我明白目的 如果我正确理解了您的问题,那么您的数组E1和E2以0:[-0.5,…,0.5]为中心,而函数f是以256为中心的高斯函数。您应该更改函数f,使其相对于数组E1和E2正确放置,或者规范化数组X,Y:
import numpy as np
import matplotlib.pyplot as plt
numtopad = 512
x = np.arange(numtopad)
y = x[:,np.newaxis]
x0 = 256
y0 = 256
f = exp( -((y-y0)**2 + (x-x0)**2)/9000)
X,Y = numpy.meshgrid(x,y)
X = ((X)/512.)-0.5
Y = ((Y)/512.)-0.5
fig = plt.figure()
axe = fig.add_subplot(111)
axe.contourf(X, Y, abs(f))
fig.show()
如果只想重新缩放数据,可以使用此代码,甚至可以使用此代码生成x和y(但必须更改f):
您好,aTben0,上面的代码只是说明问题的简单测试代码。实际代码相当长。下面是代码的基本功能。我在时域中采集了一些数据,这是一个未知函数。它是傅里叶变换的。我使用E1,E2为这个数据生成轴。它们实际上不会从负频率变为正频率,因为存在偏移频率。因此,通常fft频率在窗口中定义为-F/2:F/2。其中F为反时域步进。这里E1和E2=-F/2:F/2+偏移频率。
x = numpy.linspace(-0.5,0.5,512) #512p linearly spaced [-0.5,0.5]
X,Y = numpy.meshgrid(x,x) #2-D meshgrid on the box [-0.5,0.5]
f = exp( -((X)**2 + (Y)**2))
axe.contourf(X, Y, f)