Python 如何在一张图中显示多个图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17111525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show multiple images in one figure?
提问by Alcott
I use Python lib matplotlibto plot functions, and I know how to plot several functions in different subplots in one figure, like this one, 
我使用 Python libmatplotlib来绘制函数,并且我知道如何在一个图中绘制不同subplot 中的多个函数,例如这个,
And when handling images, I use imshow()to plot images, but how to plot multiple images together in different subplots with one figure?
在处理图像时,我使用imshow()绘制图像,但是如何将多个图像一起绘制在不同的子图中与一个图形?
回答by mgilson
The documentationprovides an example (about three quarters of the way down the page):
该文档提供了一个示例(大约在页面下方的四分之三):
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
fig = plt.figure()
a=fig.add_subplot(1,2,1)
img = mpimg.imread('../_static/stinkbug.png')
lum_img = img[:,:,0]
imgplot = plt.imshow(lum_img)
a.set_title('Before')
plt.colorbar(ticks=[0.1,0.3,0.5,0.7], orientation ='horizontal')
a=fig.add_subplot(1,2,2)
imgplot = plt.imshow(lum_img)
imgplot.set_clim(0.0,0.7)
a.set_title('After')
plt.colorbar(ticks=[0.1,0.3,0.5,0.7], orientation='horizontal')
# ---------------------------------------
# if needed inside the application logic, uncomment to show the images
# plt.show()
Basically, it's the same as you do normally with creating axes with fig.add_subplot...
基本上,它与您通常使用fig.add_subplot...
回答by Vinod Kumar K
Simple python code to plot subplots in a figure;
在图中绘制子图的简单python代码;
rows=2
cols=3
fig, axes = plt.subplots(rows,cols,figsize=(30,10))
plt.subplots_adjust(wspace=0.1,hspace=0.2)
features=['INDUS','RM', 'AGE', 'DIS','PTRATIO','MEDV']
plotnum=1
for idx in features:
plt.subplot(rows,cols,plotnum)
sns.distplot(data[idx])
plotnum=plotnum+1
plt.savefig('subplots.png')
go through below link for more detail https://exploredatalab.com/how-to-plot-multiple-subplots-in-python-with-matplotlib/
通过以下链接了解更多详情 https://exploredatalab.com/how-to-plot-multiple-subplots-in-python-with-matplotlib/

