How to make multiple plots on the same figure in Matplotlib in Python
• 1 minMatplotlib is a must when you need to plot data.
Using the matplotlib.pyplot.subplots() method you can specify the number of plots you want in your figure.
Here is an example
# DataFrame library
import pandas as pd
# Graphing library
import maptplotlib.pyplot as plt
df = pd.DataFrame({"col1":range(0,10),
"col2":range(0,10)})
# We define the main canvas with 2 rows and 1 column
# and a height of 12 inches and a width of 6 inches
fig, axes = plt.subplots(2,1, figsize=(12,6))
# We plot the col1 on the first plot
axes[0].plot(df["col1"])
# We graph the col2 on the second plot
axes[1].plot(df["col2"])
# To make the plot nice and tidy
fig.tight_layout()
# We show the plot
plt.show()
Here you are! You now know how to make multiple plots on the same figure in Matplotlib in Python.