如何在 Jupyter Notebook 中放宽 Matplotlib 绘图维度

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

我想在 python 中使用 matplotlib 比较两个图像。我的代码有效,但我想将图像大小调整为颜色条的高度。

plt.subplot(1, 2, 1)
plt.imshow(train_images[0])
plt.colorbar()
plt.subplot(1, 2, 2)
plt.imshow(train_images_prep[0])
plt.colorbar()

python python-3.x matplotlib jupyter-notebook jupyter
1个回答
0
投票

使用

imshow
使轴缩放在两个维度上相等,从而使图像成为正方形。
colorbar
的高度与重新缩放之前原始轴的高度相同。您可以从更宽的画布开始以获得所需的结果,而不是使用默认的图形尺寸:

plt.figure(figsize=(12,5))  # Dimensions in inches, play with the width

根据需要调整尺寸。我还建议使用面向对象的 API:

fig, axes = plt.subplots(2, 1, figsize=(12,5))
first = axes[0].imshow(train_images[0])
second = axes[1].imshow(train_images_prep[0])
fig.colorbar(first, ax=axes[0])
fig.colorbar(second, ax=axes[1])
© www.soinside.com 2019 - 2024. All rights reserved.