matplotlib imshow editing x-axis

倖福魔咒の 提交于 2019-12-24 02:44:07

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/12425395/matplotlib-imshow-editing-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!