Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib : How to get a triangular matrix of subplots?

I would like to have a set of subplots divided in three lines with one subplot on the first line, two on the second and three on the third. I did the following :

fig, axes = plt.subplots(figsize=(10, 10), sharex=True, sharey=True, ncols=3, nrows=3)
x = np.linspace(0, 10, 100)
for i in range(3):
    for j in range(0, i+1):
        axes[i, j].plot(x, np.sin((i+j) *x))

Thus I get : enter image description here

How can I remove the three empty plots ?

like image 542
Ger Avatar asked Oct 18 '25 21:10

Ger


1 Answers

How about this?

fig, axes = plt.subplots(figsize=(10, 10), sharex=True, sharey=True, ncols=3, nrows=3)
x = np.linspace(0, 10, 100)
for i in range(3):
    for j in range(3):
        if i<j:
            axes[i, j].axis('off')
        else:
            axes[i, j].plot(x, np.sin((i+j) *x))

It seems to produce a plot you're looking for:

enter image description here

like image 163
Vlas Sokolov Avatar answered Oct 21 '25 09:10

Vlas Sokolov