import matplotlib.pyplot as plt fig = plt.figure() # create a new figure ax = fig.add_subplot() # add a subplot to the figure ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # plot data on the subplot fig.show() # display the figure
import matplotlib.pyplot as plt fig = plt.figure() # create a new figure ax1 = fig.add_subplot(2, 1, 1) # add the first subplot ax2 = fig.add_subplot(2, 1, 2) # add the second subplot ax1.plot([1, 2, 3, 4], [1, 4, 2, 3]) # plot data on the first subplot ax2.plot([1, 2, 3, 4], [4, 2, 3, 1]) # plot data on the second subplot fig.show() # display the figureIn this example, we created a new Figure object `fig` and added two subplots to it using `add_subplot()`. We then plotted line graphs on each subplot and displayed the figure using `show()`. The package library used for this code is Python Matplotlib.