Back to Python Notes
Topic #124

Matplotlib Subplot

Subplots arrange multiple charts in one figure for side-by-side comparison. They are how dashboards show several metrics together.


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 Coding Now Tech Institute'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 Coding Now Tech Institute explains Matplotlib Subplot in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, 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 Coding Now Tech Institute'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 Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now