Search code examples
pythonmatplotlibgrid

Pyplot grid with different linewidths


i want to show a grid with different linewidths with pyplot, here is the context:

Cells do not communicate with each other. They are separated by walls of different thicknesses (there are 5 different wall thicknesses).I want it to look like this

Thanks for helping

i tried this :

rows, cols = self.lignes, self.colonnes
fig, ax = plt.subplots(rows, cols,
                       sharex='col',
                       sharey='row')

for row in range(rows):
    for col in range(cols):
        ax[row, col].text(0.5, 0.5,
                          str((row, col)),
                          color="green",
                          fontsize=18,
                          ha='center')
        plt.axis('on')

plt.show()

Solution

  • I'm not sure exactly how you want to choose your spine width for each subplot, but here's how to set the width:

    import matplotlib.pyplot as plt
    
    rows, cols = 2, 3
    fig, ax = plt.subplots(rows, cols,
                           sharex='col',
                           sharey='row')
    
    for row in range(rows):
        for col in range(cols):
            spine_proxy = ax[row, col].spines[['top', 'bottom', 'left', 'right']]
            spine_proxy.set_linewidth(row*cols+col+1)
            ax[row, col].text(0.5, 0.5,
                              str((row, col)),
                              color="green",
                              fontsize=18,
                              ha='center')
            plt.axis('on')
    
    plt.show()
    

    For more information about Matplotib's API for the spines, see the docs.