首页 > 解决方案 > 使用 matplotlib 更改子图的大小

问题描述

我正在尝试使用 matplotlib 绘制多个 rgb 图像

我正在使用的代码是:

import numpy as np
import matplotlib.pyplot as plt

for i in range(0, images):
    test = np.random.rand(1080, 720,3)
    plt.subplot(images,2,i+1)
    plt.imshow(test, interpolation='none')

虽然作为缩略图,子图看起来很小我怎样才能让它们变大?我已经看到使用的解决方案

fig, ax = plt.subplots() 

之前的语法,但不是 with plt.subplot?

标签: matplotlib

解决方案


plt.subplots启动子图网格,同时plt.subplot添加子图。因此,区别在于您是想立即开始绘制还是随着时间的推移填充它。既然您似乎事先知道要绘制多少张图像,我还建议您使用子图。

另请注意,您使用的方式会plt.subplot在您实际使用的子图之间生成 empy 子图,这是它们如此小的另一个原因。

import numpy as np
import matplotlib.pyplot as plt

images = 4


fig, axes = plt.subplots(images, 1,  # Puts subplots in the axes variable
                         figsize=(4, 10),  # Use figsize to set the size of the whole plot
                         dpi=200,  # Further refine size with dpi setting
                         tight_layout=True)  # Makes enough room between plots for labels

for i, ax in enumerate(axes):
    y = np.random.randn(512, 512)
    ax.imshow(y)
    ax.set_title(str(i), fontweight='bold')

阴谋


推荐阅读