Matplotlib批量绘制5000张图像时触发尺寸过大ValueError的问题求助
Matplotlib批量绘制5000张图像时触发尺寸过大ValueError的问题求助
大家好,我最近在做猫狗图像分类的项目,需要批量绘制数据集里的5000张图像,每张图的尺寸都是512×512。我用的是matplotlib.pyplot(代码里写的import matplotlib.pyplot as plt),不管在Google Colab还是PyCharm里跑,都会遇到同一个问题——测试100张图像的时候代码完全正常,但一尝试绘制5000张就触发了报错。
我的代码逻辑是先计算子图的行列数,再循环绘制图像,最后隐藏空的子图,具体代码如下:
# Calculate the number of rows and columns for subplots. Plot the images num_images = len(df_BW) num_rows = (num_images - 1) // 10 + 1 num_cols = min(num_images, 10) fig, axes = plt.subplots(num_rows, 10, figsize=(20, num_rows * 2)) for idx, (img, label) in enumerate(zip(df_BW['image'], df_BW['label'])): ax = axes[idx // 10, idx % 10] ax.imshow(img, cmap = 'gray') ax.axis('off') ax.set_title(label) # Hide empty subplots for i in range(num_images, num_rows * 10): axes.flatten()[i].axis('off') plt.tight_layout() plt.show()
运行5000张图像时触发的错误信息如下:
ValueError: Image size of 2000x100000 pi...
备注:内容来源于stack exchange,提问作者Fakhrul
相关产品推荐
相关产品推荐

