简体   繁体   中英

Matplotlib: contourf rescales and axes wrong when I use X,Y


Full test Code as requested:

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))

Could you give your complete code as the pictures are not available please, just to be sure that I get the purpose?

If I correctly understood your problem, your arrays E1 and E2 are centered around 0 : [-0.5,...,0.5] whereas the function f is a gaussian centered around 256. You should change your function f to be correctly placed with respect to your arrays E1 and E2 or normalize your arrays 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()

If you just want to rescale your data, you could use this code or even generate x and y with this code (but you would have to change f):

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM