Matplotlib > Multiple plots

Name
Code
Output
# Data
x = [0,1,2,3,4]
y = [1,2,3,2,3]
# New Figure
fig = matplotlib.pyplot.figure(figsize=(7,4),facecolor='silver')
# SUBPLOT 1
# New Axes with grid
ax1 = fig.add_subplot(221,facecolor='azure')
ax1.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.plot(x,y,color='red')
ax1.set(title="Plot1 title", xlabel="x axis", ylabel="y axis") 
# SUBPLOT 2
# New Axes with grid
ax2 = fig.add_subplot(222,facecolor='azure')
ax2.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.scatter(x,y,color='green')
ax2.set(title="Plot2 title", xlabel="x axis", ylabel="y axis") 
# SUBPLOT 3
# New Axes with grid
ax3 = fig.add_subplot(223,facecolor='azure')
ax3.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.bar(x,y,color='orange')
ax3.set(title="Plot3 title", xlabel="x axis", ylabel="y axis") 
# SUBPLOT 4
# New Axes with grid
ax3 = fig.add_subplot(224,facecolor='azure')
ax3.grid(color='b', linestyle='-', linewidth=0.5)
# Plot the data
matplotlib.pyplot.fill_between(x,y,color='lime')
ax3.set(title="Plot4 title", xlabel="x axis", ylabel="y axis") 
# Fit the plot layout
matplotlib.pyplot.tight_layout()
# Show the plot
matplotlib.pyplot.show()

       
           Multiple subplots