python - 在 IPython Notebook 中绘制多个子图

标签 python matplotlib ipython jupyter-notebook

我有两组 8 张图像,每组都作为 numpy 数组给出。

因为我想在我的程序中的某个时刻使用 for 循环,我想在同一个单元格中将每个集合显示为 matplotlib 子图,不幸的是它不起作用(我尝试使用 IPython.display 中的显示功能,完全没有成功)。

这是我使用的代码:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

for i, img in enumerate(set1):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

for i, img in enumerate(set2):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

它只显示第二组。

最佳答案

发生的事情是您将所有内容都绘制在同一个图形上。因此,当绘制来自 set2 的图像时,您将覆盖来自 set1 的图像。您可以通过在 for 循环之前调用 plt.figure() 来创建一个新图形:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

plt.figure()
for i, img in enumerate(set1):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

plt.figure()
for i, img in enumerate(set2):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

关于python - 在 IPython Notebook 中绘制多个子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46809103/

相关文章:

python - 如何获取 PCA 的权重

python - 为什么 multiprocessing.Lock() 不锁定 Python 中的共享资源?

python - 1064, “You have an error in your SQL syntax” 插入 MySql

python - EPD Python 和 MacPorts Python 可以在 OS X (matplotlib) 上共存吗?

ipython - 无法设置 IPython 提示

python - 将属性动态设置为Python类中的函数

python - 在另一个子图上绘制 Networkx Graph 后,轴消失

python - matplotlib - 如何绘制随机定向的矩形(或任何形状)?

bash - Docker 和 .bash_history

python - Ipython notebook 在 Ipython.Display 模块中对齐 Latex 方程