How to make an axes occupy multiple subplots with pyplot (Python)

前端 未结 5 1920
盖世英雄少女心
盖世英雄少女心 2021-01-31 14:32

I would like to have three plots in single figure. The figure should have a subplot layout of two by two, where the first plot should occupy the first two subplot cells (i.e. th

5条回答
  •  [愿得一人]
    2021-01-31 15:07

    To have multiple subplots with an axis occupy, you can simply do:

    from matplotlib import pyplot as plt
    import numpy as np
    
    b=np.linspace(-np.pi, np.pi, 100)
    
    a1=np.sin(b)
    
    a2=np.cos(b)
    
    a3=a1*a2
    
    plt.subplot(221)
    plt.plot(b, a1)
    plt.title('sin(x)')
    
    plt.subplot(222)
    plt.plot(b, a2)
    plt.title('cos(x)')
    
    plt.subplot(212)
    plt.plot(b, a3)
    plt.title('sin(x)*cos(x)')
    
    plt.show()
    

    enter image description here

    Another way is

    plt.subplot(222)
    plt.plot(b, a1)
    plt.title('sin(x)')
    
    plt.subplot(224)
    plt.plot(b, a2)
    plt.title('cos(x)')
    
    plt.subplot(121)
    plt.plot(b, a3)
    plt.title('sin(x)*cos(x)')
    
    plt.show()
    

    enter image description here

提交回复
热议问题