堆叠子图的对齐 [英] alignment of stacked subplots

查看:91
本文介绍了堆叠子图的对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一个答案(参见下文),以了解如何在其子图中对齐图像:

I found myself an answer (see below) how to align the images within their subplots:

for ax in axes:
    ax.set_anchor('W')

编辑结束

我有一些用imshow绘制的数据.它在x方向上很长,因此我通过在垂直堆叠的子图中绘制数据切片将其分成多行.我对结果感到满意,但对于最后一个子图(不如其他子图宽),我希望使其与其他子图对齐.

I have some data I plot with imshow. It's long in x direction, so I break it into multiple lines by plotting slices of the data in vertically stacked subplots. I am happy with the result but for the last subplot (not as wide as the others) which I want left aligned with the others.

下面的代码已通过Python 2.7.1和matplotlib 1.2.x进行了测试.

The code below is tested with Python 2.7.1 and matplotlib 1.2.x.

#! /usr/bin/env python

import matplotlib.pyplot as plt
import numpy as np

x_slice = [0,3]
y_slices = [[0,10],[10,20],[20,30],[30,35]]
d = np.arange(35*3).reshape((35,3)).T
vmin = d.min()
vmax = d.max()
fig, axes = plt.subplots(len(y_slices), 1)


for i, s in enumerate(y_slices):
    axes[i].imshow( 
        d[ x_slice[0]:x_slice[1], s[0]:s[1] ], 
        vmin=vmin, vmax=vmax,
        aspect='equal',
        interpolation='none'
    )

plt.show()

产生

考虑到Zhenya的技巧,我使用axis.get/set_position玩耍.我尝试将宽度减半,但我不明白它的效果

Given the tip by Zhenya I played around with axis.get/set_position. I tried to half the width but I don't understand the effect it has

for ax in axes:
    print ax.get_position()

p3 = axes[3].get_position().get_points()
x0, y0 = p3[0]
x1, y1 = p3[1]
# [left, bottom, width, height]
axes[3].set_position([x0, y0, (x1-x0)/2, y1-y0])

get_position给了我每个子图的bbox:

get_position gives me the bbox of each subplot:

for ax in axes:
    print ax.get_position()

Bbox(array([[ 0.125     ,  0.72608696],
            [ 0.9       ,  0.9       ]]))
Bbox(array([[ 0.125     ,  0.5173913 ],
            [ 0.9       ,  0.69130435]]))
Bbox(array([[ 0.125     ,  0.30869565],
            [ 0.9       ,  0.4826087 ]]))
Bbox(array([[ 0.125     ,  0.1       ],
            [ 0.9       ,  0.27391304]]))

因此所有子图具有完全相同的水平范围(0.125至0.9).从较窄的第4个子图来看,子图内的图像居中居中.

so all the subplots have the exact same horizontal extent (0.125 to 0.9). Judging from the narrower 4th subplot the image inside the subplot is somehow centered.

让我们看一下AxesImage对象:

Let's look at the AxesImage objects:

for ax in axes:
    print ax.images[0]

AxesImage(80,348.522;496x83.4783)
AxesImage(80,248.348;496x83.4783)
AxesImage(80,148.174;496x83.4783)
AxesImage(80,48;496x83.4783)

再次,第四个图像的水平范围也相同.

again, the same horizontal extent for the 4th image too.

接下来尝试AxesImage.get_extent():

Next try AxesImage.get_extent():

for ax in axes:
    print ax.images[0].get_extent()

# [left, right, bottom, top]
(-0.5, 9.5, 2.5, -0.5)
(-0.5, 9.5, 2.5, -0.5)
(-0.5, 9.5, 2.5, -0.5)
(-0.5, 4.5, 2.5, -0.5)

有一个差异(右),但是所有的左值都相同,那么为什么第四个居中呢?

there is a difference (right) but the left value is the same for all so why is the 4th one centered then?

它们都居中...

推荐答案

Axis.set_anchor到目前为止工作正常(我只是希望我现在不必手动进行过多调整):

Axis.set_anchor works so far (I just hope I don't have to adjust too much manually now):

for ax in axes:
    ax.set_anchor('W')

这篇关于堆叠子图的对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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