简体   繁体   English

如何在matplotlib子图像轴之间强制相同的大小

[英]How to force same size between matplotlib subplot image axes

Suppose I have the following code to create three side-by-side images: 假设我有以下代码来创建三个并排图像:

    n=10
    x = np.random.rand(n,1)
    y = np.random.rand(1,n)
    z = np.random.rand(n,n)

    fig, ax = plt.subplots(1, 3)
    ax[0].imshow(x)
    ax[1].imshow(z)
    ax[2].imshow(y)

However, the axes autoscale so that the vertical axis in the first image is larger than the vertical axis in the second. 但是,轴是自动缩放的,因此第一个图像中的垂直轴大于第二个中的垂直轴。

在此输入图像描述

Is there a way to programmatically force all image dimensions of size n to look the same in the three plots, regardless of window size? 有没有办法以编程方式强制大小为n所有图像尺寸在三个图中看起来相同,而不管窗口大小? I'm looking for a way to either link the axes or the images so that the vertical axis of the first plot is the same size as the vertical axis of the second plot, and the horizontal axis of the third plot is the same size as the horizontal axis of the second plot, regardless of window size. 我正在寻找一种方法来链接轴或图像,以便第一个图的垂直轴与第二个图的垂直轴大小相同,第三个图的水平轴与第二个图的水平轴,与窗口大小无关。 ie something like this: 即是这样的: 在此输入图像描述

I think one easiest way is to use aspect='auto' with ax[1].imshow(z) . 我认为最简单的方法是使用aspect='auto' ax[1].imshow(z) aspect='auto' ax[1].imshow(z) aspect='auto'使用ax[1].imshow(z) But this will distort the image in a way that may be not the same as what you've shown in the question. 但这会使图像扭曲,可能与您在问题中显示的方式不同。 And it may not work for cases where there is no single n . 并且它可能不适用于没有单个n I'm not sure if I got you 100%, but let me try this method. 我不确定我是否100%给你,但让我尝试这种方法。 The key idea here are: 这里的关键想法是:

  1. Change the aspect ratio of your fig . 改变fig的纵横比。 The exact ratio comes from both your image data and your subplot layout. 确切的比例来自您的图像数据和子图布局。
  2. Use tight layout to eliminate unnecessary between axes which may offset your graph a little bit. 使用严格的布局来消除轴之间不必要的,这可能会略微偏移图形。

Here is my example code and figure: 这是我的示例代码和图:

import matplotlib.pyplot as plt
from matplotlib.figure import figaspect
import numpy as np

n = 10
x = np.random.rand(n,1)
y = np.random.rand(1,n)
z = np.random.rand(n,n)

width_max = max(s.shape[0] for s in [x, y, z])
height_max = max(s.shape[1] for s in [x, y, z])

row = 1
col = 3
fig, ax = plt.subplots(row, col)
w, h = figaspect(row*width_max/(col*height_max))
fig.set_size_inches(w, h)

ax[0].imshow(x)
ax[1].imshow(z)
ax[2].imshow(y)

plt.tight_layout()
plt.show()

在此输入图像描述

I hope this solves your real problem. 我希望这能解决你真正的问题。 I think this also works for a case like: 我认为这也适用于以下情况:

x = np.random.rand(3,1)
y = np.random.rand(1,10)
z = np.random.rand(7,6)

You may restrict your figure size and/or the subplot parameters in the vertical direction to leave less space for the axes to scale. 您可以在垂直方向上限制图形大小和/或子图参数,以便为轴缩放留出更少的空间。

fig, ax = plt.subplots(1, 3, figsize=(6.4,3))
fig.subplots_adjust(bottom=0.26, top=0.74)

情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM