9

How do I plot 5 subplots instead of 6 plots as a output to a jupyter notebook cell. I'm trying to plot 5 separate subplots but when I tried using,

fig, ax = plt.subplots(2, 3, figsize=(10, 7))

This is returning 6 subplots in two rows and three columns. What I'm trying to achieve is plotting 5 subplots in two rows?

Eg: In first row I need three subplots and in second row I need only two subplots instead of there.

How can I achieve this using matplotlib or seaborn?

user24046
  • 93
  • 1
  • 1
  • 6

2 Answers2

9
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure(figsize=(10,7))
columns = 3
rows = 2
a=np.random.rand(2,3)
for i in range(1, 6):
    fig.add_subplot(rows, columns, i)
    plt.plot(a)### what you want you can plot  
plt.show()

output

Rahul Verma
  • 2,988
  • 2
  • 11
  • 26
2

You can also try the using gridspec Click here to see the image

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=False)
x = np.arange(0, 10, 0.2)
y = np.sin(x)
gs = gridspec.GridSpec(2, 3, figure=fig)
ax1 = fig.add_subplot(gs[0, 0])
# identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3))
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])
ax5 = fig.add_subplot(gs[1, 2])
ax1.plot(x, y)
ax2.plot(x, y)
ax3.plot(x, y)
ax4.plot(x, y)
ax5.plot(x, y)
fig.suptitle("GridSpec")

plt.show()