bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Python/Data Science and Scientific Python
Python•Data Science and Scientific Python

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()

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()

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()

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()

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()

Previous

Machine Learning - Linear Regression

Next

Machine Learning - Polynomial Regression

This chapter

Overview
38

Lessons

159m

Read time

1. NumPy Tutorial2. Matplotlib Tutorial3. Machine Learning4. Pandas Tutorial5. Matplotlib Getting Started6. Machine Learning - Mean Median Mode7. Matplotlib Pyplot8. Machine Learning - Standard Deviation9. Matplotlib Plotting10. Machine Learning - Percentiles11. Matplotlib Markers12. Machine Learning - Data Distribution13. Matplotlib Line14. Machine Learning - Normal Data Distribution15. Matplotlib Labels and Title16. Machine Learning - Scatter Plot17. Matplotlib Adding Grid Lines18. Machine Learning - Linear Regression19. Matplotlib Subplot20. Machine Learning - Polynomial Regression21. Matplotlib Scatter22. Machine Learning - Multiple Regression23. Matplotlib Bars24. Machine Learning - Scale25. Matplotlib Histograms26. Machine Learning - Train/Test27. Matplotlib Pie Charts28. Machine Learning - Decision Tree29. Machine Learning - Confusion Matrix30. Machine Learning - Hierarchical Clustering31. Machine Learning - Logistic Regression32. Machine Learning - Grid Search33. Preprocessing - Categorical Data34. Machine Learning - K-means35. Machine Learning - Bootstrap Aggregation (Bagging)36. Machine Learning - Cross Validation37. Machine Learning - AUC - ROC Curve38. Machine Learning - K-nearest neighbors (KNN)

On this page

Display Multiple PlotsThe subplot() FunctionSuper Title