Search code examples
pythonpython-3.xmatplotlibplotsubfigure

Create background for a 3D and 2D plots


Can someone help me create a layout for plots with the following structure:

A   CD
A/B EF
B   GH

where A,B are 3D plots (fig.add_subplot(241, projection='3d')) and C,D,E,F,G,H are regular 2D plots. A/B represents a shared space where half of plot A and half of plot B appear.


Solution

  • You could use a subplot_mosaic:

    f, grid = plt.subplot_mosaic('''\
    ACD
    ACD
    AEF
    BEF
    BGH
    BGH''', per_subplot_kw={('A', 'B'): {'projection': '3d'}} )
    
    plt.tight_layout()
    

    Then plot on grid['A']/grid['B']/grid['C']/...

    Output:

    matplotlib subplot mozaic 2D 3D