Search code examples
pythonmatplotlibsubplot

Matplotlib One plot side with multiple subplot


I am trying to plot some graphs, I want it to look like that, and do it in python.

I tried many things but it looks way uglier than what I want to achieve, any help would be appreciated

enter image description here


Solution

  • You can use add_grid_spec(), add_subplot(), and subgridspec(). (matplotlib.__version__ >= '3.5.0')

    # Please Update matplotlib
    # !pip install matplotlib --upgrade
    
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    outer = fig.add_gridspec(nrows=1, ncols=2)
    ax1 = fig.add_subplot(outer[0, 0])
    
    inner = outer[0, 1].subgridspec(ncols=2, nrows=2, wspace=0.5)
    ax2 = inner.subplots()
    
    
    ax1.plot(range(10), 'r')
    ax2[0,0].plot(range(20), 'g')
    ax2[0,1].plot(range(10), 'r')
    ax2[1,0].plot(range(5), 'b')
    ax2[1,1].plot(range(10), 'y')
        
        
    fig.tight_layout()
    plt.show()
    

    Output:

    enter image description here