您可以基于的subplots命令(请注意末尾的
s ,与
subploturinieto指向的命令不同)来定义函数
matplotlib.pyplot。
下面是基于您的函数的一个示例,允许在图中绘制多个轴。您可以在图形布局中定义所需的行数和列数。
def plot_figures(figures, nrows = 1, ncols=1): """Plot a dictionary of figures. Parameters ---------- figures : <title, figure> dictionary ncols : number of columns of subplots wanted in the display nrows : number of rows of subplots wanted in the figure """ fig, axeslist = plt.subplots(ncols=ncols, nrows=nrows) for ind,title in enumerate(figures): axeslist.ravel()[ind].imshow(figures[title], cmap=plt.gray()) axeslist.ravel()[ind].set_title(title) axeslist.ravel()[ind].set_axis_off() plt.tight_layout() # optional
基本上,该函数根据所需的行(
nrows)和列(
ncols)的数量在图中创建多个轴,然后在轴列表上进行迭代以绘制图像并为每个图像添加标题。
请注意,如果词典中只有一个图像,则以前的语法
plot_figures(figures)从那时起将起作用,
nrows并且默认
ncols设置为
1。
您可以获取的示例:
import matplotlib.pyplot as pltimport numpy as np# generation of a dictionary of (title, images)number_of_im = 6figures = {'im'+str(i): np.random.randn(100, 100) for i in range(number_of_im)}# plot of the images in a figure, with 2 rows and 3 columnsplot_figures(figures, 2, 3)


