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

前端 未结 5 1921
盖世英雄少女心
盖世英雄少女心 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

    0 讨论(0)
  • 2021-01-31 15:14

    There are three main options in matplotlib to make separate plots within a figure:

    1. subplot: access the axes array and add subplots
    2. gridspec: control the geometric properties of the underlying figure (demo)
    3. subplots: wraps the first two in a convenient api (demo)

    The posts so far have addressed the first two options, but they have not mentioned the third, which is the more modern approach and is based on the first two options. See the specific docs Combining two subplots using subplots and GridSpec.

    0 讨论(0)
  • 2021-01-31 15:20

    The Using Gridspec to make multi-column/row subplot layouts shows a way to do this with GridSpec. A simplified version of the example with 3 subplots would look like

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    
    gs = fig.add_gridspec(2,2)
    ax1 = fig.add_subplot(gs[0, 0])
    ax2 = fig.add_subplot(gs[0, 1])
    ax3 = fig.add_subplot(gs[1, :])
    
    plt.show()
    

    0 讨论(0)
  • 2021-01-31 15:22

    For finer-grained control you might want to use the subplot2grid module of matplotlib.pyplot.

    http://matplotlib.org/users/gridspec.html

    0 讨论(0)
  • 2021-01-31 15:27

    You can simply do:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.arange(0, 7, 0.01)
        
    plt.subplot(2, 1, 1)
    plt.plot(x, np.sin(x))
        
    plt.subplot(2, 2, 3)
    plt.plot(x, np.cos(x))
        
    plt.subplot(2, 2, 4)
    plt.plot(x, np.sin(x)*np.cos(x))
    

    i.e., the first plot is really a plot in the upper half (the figure is only divided into 21 = 2 cells), and the following two smaller plots are done in a 22=4 cell grid. The third argument to subplot() is the positon of the plot inside the grid: for example in the second subplot (subplot(2, 2, 3)), the axes will go to the third section of the 2*2 matrix i.e, to the bottom-left corner.

    0 讨论(0)
提交回复
热议问题