将轴尺寸限制为另一个轴的尺寸

问题描述 投票:0回答:1

[我有代码通过使用GridSpecwidth_ratiosheight_ratios生成在主图像图的左侧和下方具有较小图的正方形图像:

plot with secondary axes left and below the main image axis

import matplotlib.pyplot as plt
import numpy as np

# Some fake data.
imdata = np.random.random((100, 100))
extradata1 = np.max(imdata, axis=1)
extradata2 = np.max(imdata, axis=0)

fig = plt.figure(constrained_layout=True)
spec = fig.add_gridspec(ncols=2, nrows=2, width_ratios=(1, 8), height_ratios=(8, 1))

# Main image plot.
ax1 = fig.add_subplot(spec[:-1, 1:], aspect='equal')
ax1.imshow(imdata, cmap='viridis')

# Vertical (left) plot.
ax2 = fig.add_subplot(spec[:-1, 0], sharey=ax1)
ax2.plot(extradata1, range(imdata.shape[0]))

# Horizontal (bottom) plot.
ax3 = fig.add_subplot(spec[-1, 1:], sharex=ax1)
ax3.plot(range(imdata.shape[1]), extradata2)

plt.show()

我希望左侧图的高度和底部图的宽度分别等于主图像的高度和宽度。目前,您可以看到水平图的宽度大于图像的水平尺寸,并且它们在缩放比例时也不同。是否可以将轴尺寸限制为其他轴的尺寸?

python matplotlib plot imshow
1个回答
0
投票

imshow()调用aspect='auto'应该可以解决您的问题:

ax1.imshow(imdata, cmap='viridis',aspect='auto')

有关此内容的更多说明,请参见此处:Imshow: extent and aspect

© www.soinside.com 2019 - 2024. All rights reserved.