Home > Software engineering >  Changing axis values with imshow and showing data points
Changing axis values with imshow and showing data points

Time:07-30

I would like to properly change the axes so I can see the values of the x and y components of the two frequencies. I have two sets of code: the first shows the proper data but the axes are wrong, the second shows the proper axes but my two data points are not showing.

First code:

from scipy.fft import fft2, fftshift
import numpy as np
import matplotlib.pyplot as plt
from skimage.filters import window
from scipy.fftpack import fftfreq


k = np.linspace(0,4.76*10,2400)
kx,ky = np.meshgrid(k, k)
x1 = 0.3
y1 = 0.4
x2 = 0.3
y2 = 1

z = 0.05*np.cos(2*np.pi*kx*x1   2*np.pi*ky*y1)   0.05*np.cos(2*np.pi*kx*x2   2*np.pi*ky*y2)

wz = z * window('hann', z.shape)


zf = np.abs(fftshift(fft2(wz)))[1200:, 1200:]
plt.figure(1)
plt.axis([0,100, 0,100])

plt.imshow(zf)
plt.show()

And the results are:

enter image description here

The second code:

from scipy.fft import fft2, fftshift
import numpy as np
import matplotlib.pyplot as plt
from skimage.filters import window
from scipy.fftpack import fftfreq


k = np.linspace(0,4.76*10,2400)
kx,ky = np.meshgrid(k, k)
x1 = 0.3
y1 = 0.4
x2 = 0.3
y2 = 1

z = 0.05*np.cos(2*np.pi*kx*x1   2*np.pi*ky*y1)   0.05*np.cos(2*np.pi*kx*x2   2*np.pi*ky*y2)

wz = z * window('hann', z.shape)


zf = np.abs(fftshift(fft2(wz)))[1200:, 1200:]
fig, ax = plt.subplots()
ax.set(xlim=(0, 2), ylim=(0, 2))

f = fftfreq(len(k), np.diff(k)[0])
ax.imshow(zf,extent=[0,f[:k.size//2][-1], 0 , f[:k.size//2][-1]])
plt.show()

enter image description here

CodePudding user response:

You need to change the origin in the second example:

ax.imshow(zf, origin='lower', extent=[0,f[:k.size//2][-1], 0 , f[:k.size//2][-1]])

enter image description here

  • Related