编辑:
我发现自己是一个答案(见下文)如何在子图中对齐图像:
for ax in axes:
ax.set_anchor('W')
编辑结束
我有一些我用imshow绘制的数据.它在x方向很长,所以我通过在垂直堆叠的子图中绘制数据切片将其分成多行.我对结果感到满意,但最后一个子图(不像其他子图那样宽)我希望与其他子图对齐.
下面的代码使用Python 2.7.1和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.我试过宽度的一半,但我不明白它的效果
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:
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个子图中判断,子图内的图像以某种方式居中.
我们来看看AxesImage对象:
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)
再次,第四张图像的水平范围也相同.
接下来尝试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)
有一个区别(右)但是左边的值对所有人都是一样的,那么为什么第四个中心呢?
编辑:他们都集中在……