Python Forum
Getting proper x,y axis values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting proper x,y axis values (/thread-37855.html)



Getting proper x,y axis values - pyhill00 - Jul-29-2022

I am trying to change the values of the axes so that the x and y values (0.3,0.4) and (0.3,1) are shown on the axes for the points. Anyone know how to properly do this?
    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)

    plt.figure(0)
    plt.imshow(wz)

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

    plt.imshow(zf)
    plt.show()



RE: Getting proper x,y axis values - deanhystad - Jul-29-2022

Why? Normally axis labels indicate range with maybe some subdivisions to give approximate value. If you want the real value you click on the plot.

You can tell matplotlib where to put tickmarks like this:
import numpy as np
import matplotlib.pyplot as plt
from math import sin

x = np.linspace(0.0, np.pi * 2, 100)
x1 = 1
y1 = sin(1)
x2 = 5
y2 = sin(5)

fig, ax = plt.subplots()
plt.plot(x, np.sin(x))
ax.set_xticks((x1, x2))
ax.set_yticks((y1, y2))
ax.grid(True)
plt.show()



RE: Getting proper x,y axis values - pyhill00 - Jul-29-2022

(Jul-29-2022, 04:05 PM)deanhystad Wrote: Why? Normally axis labels indicate range with maybe some subdivisions to give approximate value. If you want the real value you click on the plot.

It seems I got the axes right but now the points are not showing up. Do you know whats wrong with my code here?

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)

plt.figure(0)
plt.imshow(wz)

f = fftfreq(len(k), np.diff(k)[0])
zf = np.abs(fftshift(fft2(wz)))[1200:, 1200:]
fig, ax = plt.subplots()
ax.set(xlim=(0, 2), ylim=(0, 2))
ax.imshow(zf,extent=[0,f[:k.size//2][-1], 0 , f[:k.size//2][-1]])
plt.show()



RE: Getting proper x,y axis values - deanhystad - Jul-29-2022

What do you mean "points"?


RE: Getting proper x,y axis values - pyhill00 - Jul-29-2022

(Jul-29-2022, 05:07 PM)deanhystad Wrote: What do you mean "points"?

My original post has points in the second plot.


RE: Getting proper x,y axis values - deanhystad - Jul-29-2022

Both times you are drawing an image. That is what imshow() does. The image may be generated from data, but you are still plotting an image. I don't know why you would want to plot points that way since it removes all the point information.


RE: Getting proper x,y axis values - pyhill00 - Jul-29-2022

The goal of imshow is to show the frequencies...not sure what you are trying to get it. It's pretty obvious I want to see where the frequency plots are of an fft.


RE: Getting proper x,y axis values - deanhystad - Jul-29-2022

So when you say you cannot see the "points" does that mean that you cannot see the FFT frequency plot at all? Be aware that I cannot run your example, so all I know about the visibility of "points" is what you have posted.


RE: Getting proper x,y axis values - pyhill00 - Jul-29-2022

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

origin was not on the bottem left -_-