如何在 matplotlib 中设置纵横比? [英] How can I set the aspect ratio in matplotlib?

查看:78
本文介绍了如何在 matplotlib 中设置纵横比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作方形图(使用 imshow),即 1:1 的纵横比,但我不能.这些都不起作用:

I'm trying to make a square plot (using imshow), i.e. aspect ratio of 1:1, but I can't. None of these work:

import matplotlib.pyplot as plt

ax = fig.add_subplot(111,aspect='equal')
ax = fig.add_subplot(111,aspect=1.0)
ax.set_aspect('equal')
plt.axes().set_aspect('equal')

这些调用似乎只是被忽略了(我经常在使用 matplotlib 时遇到这个问题).

It seems like the calls are just being ignored (a problem I often seem to have with matplotlib).

推荐答案

魅力三倍.我的猜测是这是一个错误,Zhenya 的回答表明它已修复在最新版本中.我有 0.99.1.1 版,我创建了以下解决方案:

Third times the charm. My guess is that this is a bug and Zhenya's answer suggests it's fixed in the latest version. I have version 0.99.1.1 and I've created the following solution:

import matplotlib.pyplot as plt
import numpy as np

def forceAspect(ax,aspect=1):
    im = ax.get_images()
    extent =  im[0].get_extent()
    ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)

data = np.random.rand(10,20)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data)
ax.set_xlabel('xlabel')
ax.set_aspect(2)
fig.savefig('equal.png')
ax.set_aspect('auto')
fig.savefig('auto.png')
forceAspect(ax,aspect=1)
fig.savefig('force.png')

这是force.png":

This is 'force.png':

以下是我失败的尝试,但希望能提供有用的信息.

Below are my unsuccessful, yet hopefully informative attempts.

第二个答案:

我在下面的原始答案"有点矫枉过正,因为它的作用类似于 axes.set_aspect().我认为您想使用 axes.set_aspect('auto').我不明白为什么会这样,但它为我生成了一个方形图像图,例如这个脚本:

My 'original answer' below is overkill, as it does something similar to axes.set_aspect(). I think you want to use axes.set_aspect('auto'). I don't understand why this is the case, but it produces a square image plot for me, for example this script:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(10,20)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data)
ax.set_aspect('equal')
fig.savefig('equal.png')
ax.set_aspect('auto')
fig.savefig('auto.png')

生成具有相等"纵横比的图像图:和一个具有自动"纵横比的:

Produces an image plot with 'equal' aspect ratio: and one with 'auto' aspect ratio:

下面在原始答案"中提供的代码为明确控制的纵横比提供了一个起点,但一旦调用 imshow 似乎就会被忽略.

The code provided below in the 'original answer' provides a starting off point for an explicitly controlled aspect ratio, but it seems to be ignored once an imshow is called.

原始答案:

以下是一个例程示例,该例程将调整子图参数以便您获得所需的纵横比:

Here's an example of a routine that will adjust the subplot parameters so that you get the desired aspect ratio:

import matplotlib.pyplot as plt

def adjustFigAspect(fig,aspect=1):
    '''
    Adjust the subplot parameters so that the figure has the correct
    aspect ratio.
    '''
    xsize,ysize = fig.get_size_inches()
    minsize = min(xsize,ysize)
    xlim = .4*minsize/xsize
    ylim = .4*minsize/ysize
    if aspect < 1:
        xlim *= aspect
    else:
        ylim /= aspect
    fig.subplots_adjust(left=.5-xlim,
                        right=.5+xlim,
                        bottom=.5-ylim,
                        top=.5+ylim)

fig = plt.figure()
adjustFigAspect(fig,aspect=.5)
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))

fig.savefig('axAspect.png')

这会产生一个像这样的图:

This produces a figure like so:

我可以想象,如果您在图中有多个子图,您可能希望将 y 和 x 子图的数量作为关键字参数(每个默认为 1)包含在所提供的例程中.然后使用这些数字和 hspacewspace 关键字,您可以使所有子图具有正确的纵横比.

I can imagine if your having multiple subplots within the figure, you would want to include the number of y and x subplots as keyword parameters (defaulting to 1 each) to the routine provided. Then using those numbers and the hspace and wspace keywords, you can make all the subplots have the correct aspect ratio.

这篇关于如何在 matplotlib 中设置纵横比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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