display two png images simultaneously using pylab

时光怂恿深爱的人放手 提交于 2019-12-05 01:07:11

The following works for me (you can comment/uncomment the lines in the code to change the layout of the "composite" image):

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import numpy as np
import pylab
import matplotlib.cm as cm
import Image

f = pylab.figure()
for n, fname in enumerate(('1.png', '2.png')):
    image=Image.open(fname).convert("L")
    arr=np.asarray(image)
    f.add_subplot(2, 1, n)  # this line outputs images on top of each other
    # f.add_subplot(1, 2, n)  # this line outputs images side-by-side
    pylab.imshow(arr,cmap=cm.Greys_r)
pylab.title('Double image')
pylab.show()

EDIT: screenshot:

Here is an example that includes two subgraphs in one figure.

import pylab

coordinates = range(5)
x = [1,4,5,6,2]
y = [4,6,8,9,2]

fig = pylab.figure()

fig_1 = fig.add_subplot(2,1,1)

fig_1.bar(coordinates,x)

fig_2 = fig.add_subplot(2,1,2)

fig_2.bar(coordinates,y)

pylab.show()

I think you just need to combine this with what you already have.

Combining the answers above into some code that I now use:

`

import pylab as P
import numpy as N
import Image

fnames = ['1.png', '2.png', '3.png']

fig = P.figure()
for i,fname in enumerate(fnames):
    fig.add_subplot(number_images, 1, i+1)
    P.imshow(N.asarray(Image.open(fname).convert("L")))
P.show()

`

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!