matplotlib imshow编辑x轴 [英] matplotlib imshow editing x-axis

查看:230
本文介绍了matplotlib imshow编辑x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示图像的振幅谱.我可以使用以下代码来做到这一点:

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

在imshow绘图的情况下,我希望x轴具有相同的标签.这可能吗?

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

推荐答案

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

set_extent设置x和y轴的最大值和最小值.文档如下:

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.

这篇关于matplotlib imshow编辑x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆