🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #124

Matplotlib Subplot


Display Multiple Plots

With the subplot() function you can draw multiple plots in one figure:

Example

  import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x =
  np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
  plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
  40])

plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()

image


The subplot() Function

The subplot() function takes three arguments that describes the layout of the figure.

The layout is organized in rows and columns, which are represented by the first and second argument.

The third argument represents the index of the current plot.

So, if we want a figure with 2 rows an 1 column (meaning that the two plots will be displayed on top of each other instead of side-by-side), we can write the syntax like this:

Example

  import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x =
  np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 1, 1)
  plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
  40])

plt.subplot(2, 1, 2)
plt.plot(x,y)

plt.show()

image

You can draw as many plots you like on one figure, just descibe the number of rows, columns, and the index of the plot.

Example

  import matplotlib.pyplot as plt
import numpy as np

x = np.array([0,
  1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 1)
  plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
  40])

plt.subplot(2, 3, 2)
plt.plot(x,y)

x = np.array([0, 1,
  2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

  plt.subplot(2, 3, 4)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y =
  np.array([3, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x,y)

x
  = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2,
  3, 6)
plt.plot(x,y)

plt.show()

image


Title

You can add a title to each plot with the title() function:

Example

  import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x =
  np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
  plt.plot(x,y)
plt.title("SALES")

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
  40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.show()

image


Super Title

You can add a title to the entire figure with the suptitle() function:

Example

  import matplotlib.pyplot as plt
import numpy as np

#plot 1:
x =
  np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
  plt.plot(x,y)
plt.title("SALES")

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
  40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.suptitle("MY SHOP")
plt.show()

image

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Matplotlib Subplot – FAQs

Quick answers about learning Matplotlib Subplot in Python.

This free note from CodingNow 2.0 explains Matplotlib Subplot in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Matplotlib Subplot, is 100% free with no signup required.
With focused practice, most students grasp Matplotlib Subplot in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now