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

查看:680
本文介绍了如何在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.

第二个答案:

Second Answer:

我在下面的原始答案"有些过分,因为它的作用类似于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.

原始答案:

Original Answer:

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

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天全站免登陆