How to show multiple images in one figure?

前端 未结 2 1967
予麋鹿
予麋鹿 2021-01-01 10:20

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,

2条回答
  •  不知归路
    2021-01-01 11:02

    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...

提交回复
热议问题