Search code examples
matplotliblabelfftaxes

matplotlib imshow editing x-axis


I want to display an amplitude spectrum of an image. I am able to do this using following code:

import numpy as np
import matplotlib.pyplot as plt
import pylab

pylab.gray()
pic = pylab.imread("C:/pic.png")[::-1,:]
amp_pic = pylab.subplot(1,4,1)
amp_pic.xaxis.set_ticks_position('top')
pylab.imshow(np.abs(np.fft.fftshift(np.fft.fft2(pic))),\
         interpolation='nearest')
pylab.show()

But the axis is not labeled the way an amplitude spectrum should be labeled. In the case of a 1D-function the relabelling is somewhat easier:

import math
import numpy as np
import matplotlib.pyplot as plt

array = np.arange(149)
frequency_scale = np.fft.fftfreq(array.shape[0])
function = np.cos(2*math.pi*array/10)
fft = np.fft.fft(function)
amp_fft = np.abs(fft)
plt.subplot(1,4,1)
plt.plot(fkt,'r')
plt.show()

I want the same labelling for my xaxis in the case of a imshow plot. Is this possible?


Solution

  • figure()
    im = imshow(rand(500,500))
    im.set_extent([0,1,0,1])
    

    set_extent sets the maximum and minimum of the x and y axes. Doc is as follows:

     |  set_extent(self, extent)
     |      extent is data axes (left, right, bottom, top) for making image plots
     |      
     |      This updates ax.dataLim, and, if autoscaling, sets viewLim
     |      to tightly fit the image, regardless of dataLim.  Autoscaling
     |      state is not changed, so following this with ax.autoscale_view
     |      will redo the autoscaling in accord with dataLim.