此问题在此处已有答案:
How to plot images in subplots(1个答案)
Eliminate white space between subplots in a matplotlib figure(1个答案)
How to remove gaps between image subplots(2个答案)
How to combine gridspec with plt.subplots() to eliminate space between rows of subplots(1个答案)
How to remove the space between subplots(5个回答)
11天前关闭。
我想在一个10列和8行,并从垂直和水平使用python连接到彼此的一个图80images
预计这一产出
enter image description here的
这是我的代码,
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os
from PIL import Image
# Specify the path to the folder containing the images
image_folder = '/Users/shaimaa/Downloads/Speaker_image.fld'
# Get a list of image file names in the folder
image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]
# Create a 10x8 grid using gridspec
fig = plt.figure(figsize=(16, 20))
gs = gridspec.GridSpec(8, 10)
# Initialize an index to keep track of the current image
index = 0
for i in range(8):
for j in range(10):
if index < len(image_files):
image_path = os.path.join(image_folder, image_files[index])
img = Image.open(image_path)
img = img.resize((50, 50)) # Resize the image to 50x50 pixels
ax = plt.subplot(gs[i, j])
ax.imshow(img)
ax.axis('off') # Turn off the axis labels
index += 1
# Remove space between subplots (both vertically and horizontally)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
字符串
多谢帮忙
1条答案
按热度按时间ctrmrzij1#
我能想到的两个选择:
1.直接使用plt.subplots:
字符串
然而,这仍然留下了一些小的差距(原因我不知道)
两个完全避免这些差距,你可以
型