Python 如何在不拉伸图像的情况下更改matplotlib中imshow的比例?

Python 如何在不拉伸图像的情况下更改matplotlib中imshow的比例?,python,matplotlib,Python,Matplotlib,我想以类似于第二个示例的方式使用imshow进行绘图,但要重新定义轴的比例。我还希望图像在我这样做时保持静止 示例中的代码如下所示: from scipy import * from pylab import * # Creating the grid of coordinates x,y x,y = ogrid[-1.:1.:.01, -1.:1.:.01] z = 3*y*(3*x**2-y**2)/4 + .5*cos(6*pi * sqrt(x**2 +y**2) + arctan

我想以类似于第二个示例的方式使用imshow进行绘图,但要重新定义轴的比例。我还希望图像在我这样做时保持静止

示例中的代码如下所示:

from scipy import *
from pylab import *

# Creating the grid of coordinates x,y 
x,y = ogrid[-1.:1.:.01, -1.:1.:.01]

z = 3*y*(3*x**2-y**2)/4 + .5*cos(6*pi * sqrt(x**2 +y**2) + arctan2(x,y))

hold(True)
# Creating image
imshow(z, origin='lower', extent=[-1,1,-1,1])

xlabel('x')
ylabel('y')
title('A spiral !')

# Adding a line plot slicing the z matrix just for fun. 
plot(x[:], z[50, :])

show()
如果我将范围修改为更宽,例如:

imshow(z, origin='lower', extent=[-4,4,-1,1])
然后拉伸生成的图像。但我想做的就是改变刻度,使其与我的数据一致。我知道我可以使用pcolor保存X和Y数据,尽管这还有其他影响

我找到了这个答案,它允许我手动重做所有的记号:

但这似乎有点过分了

有没有办法只更改标签显示的范围?

a
help(imshow)
将找到
aspect
参数,经过一点实验后,该参数似乎给出了您想要的结果(螺旋的方形图像,但x比例从-4到4,y比例从-1到1):

imshow(z, origin='lower', extent=[-4,4,-1,1], aspect=4)
但是现在您的
绘图仍然是从-1到1,所以您也必须修改它

plot(x[:]*4, z[50, :])
我认为,当您有几个元素需要修改时,仅使用一行勾号重新标记并不过分:

xticks(xticks()[0], [str(t*4) for t in xticks()[0]])

Aspect对于我的用例来说是一个很好的解决方案,因为我在那里没有绘图。你用比我更优雅的解决方案来修复XTick。非常感谢。