
[英]Unmatching axes ticks and tick labels for a panel plot using AxesGrid
[英]Unexpected result while using AxesGrid
这段代码
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
mat0 = [[1, 2], [3, 4], [5, 6], [7, 8]] # 4 rows × 2 columns
mat1 = [[-2, 0, 2, 4], [0, 2, 4, 6]] # 2 rows × 4 columns
fig = plt.figure(figsize=(9, 3))
grid = AxesGrid(fig, 111, nrows_ncols=(1,2),
axes_pad=0.15,
cbar_size="6%", cbar_location="right", cbar_mode="single")
for ax, mat in zip(grid.axes_all, (mat0, mat1)): im = ax.imshow(mat)
grid.cbar_axes[0].colorbar(im)
plt.figure()
plt.imshow(mat0)
plt.colorbar()
plt.show()
产生两个数字
我希望在第一个图中看到左侧有一个高矩形,如第二个图所示。
当然,我不了解 AxesGrid 到底发生了什么。
我怎样才能并排放置两个图像,而不截断高大的图像?
一张图片值1000字吗?
如果您要处理不同尺寸的图像,在保持宽高比的同时以相同的高度并排显示它们可能会很棘手。 一个更简单的替代方案,对我来说是一个更优雅的解决方案,可能是只使用子图并共享颜色条。 以下是您的操作方法:
import matplotlib.pyplot as plt
import numpy as np
mat0 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) # 4 rows × 2 columns
mat1 = np.array([[-2, 0, 2, 4], [0, 2, 4, 6]]) # 2 rows × 4 columns
fig, axs = plt.subplots(1, 2, figsize=(9, 3))
cax = axs[0].imshow(mat0)
axs[1].imshow(mat1)
fig.colorbar(cax, ax=axs.ravel().tolist(), shrink=0.5)
plt.show()
在此脚本中, subplots
创建一个 1x2 轴网格。 使用imshow
将图像显示在这些轴上。 在fig.colorbar
的帮助下,颜色条在子图中共享。 shrink
参数调整颜色条的大小。 您可能需要调整figsize
参数以更好地适应您的数据和显示大小。
我希望这有帮助。
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.