matplotlib imshow编辑x轴

时间:2012-09-14 13:17:13

标签: matplotlib label fft axes

我想显示图像的幅度谱。我可以使用以下代码执行此操作:

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

但轴未标记振幅谱应标记的方式。在1D功能的情况下,重新标记更容易:

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

在imshow情节的情况下,我想为我的xaxis添加相同的标签。这可能吗?

1 个答案:

答案 0 :(得分:4)

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

set_extent设置x和y轴的最大值和最小值。 Doc如下:

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