28

I use Python lib matplotlib to plot functions, and I know how to plot several functions in different subplots in one figure, like this one, enter image description here

And when handling images, I use imshow() to plot images, but how to plot multiple images together in different subplots with one figure?

Alcott
  • 17,905
  • 32
  • 116
  • 173
  • sorry, don't understand "plot multiple images together in different subplots with one figure" Can you draw something using ascii-art or describe some more please... – Fredrik Pihl Jun 14 '13 at 15:04
  • 1
    @FredrikPihl, I mean there is one figure with several subgraphs in it, each subgraph is an image. – Alcott Jun 14 '13 at 15:18

2 Answers2

44

The documentation provides 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...

user3666197
  • 1
  • 6
  • 50
  • 92
mgilson
  • 300,191
  • 65
  • 633
  • 696
0

Simple python code to plot subplots in a figure;

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/