import matplotlib.pyplot as plt fig = plt.figure(figsize=(10, 6)) gs = fig.add_gridspec(nrows=2, ncols=2, height_ratios=[2, 1], width_ratios=[1, 2]) ax1 = fig.add_subplot(gs[0, 0]) ax1.set_title('First Plot') ax2 = fig.add_subplot(gs[0, 1]) ax2.set_title('Second Plot') ax3 = fig.add_subplot(gs[1, :]) ax3.set_title('Third Plot') plt.show()
import matplotlib.pyplot as plt fig = plt.figure(figsize=(8, 6)) gs = fig.add_gridspec(nrows=3, ncols=3, width_ratios=[1, 2, 3], height_ratios=[3, 2, 1]) ax1 = fig.add_subplot(gs[0, :]) ax1.set_title('Top Row') ax2 = fig.add_subplot(gs[1, 0]) ax2.set_title('Middle Left') ax3 = fig.add_subplot(gs[1, 1:]) ax3.set_title('Middle Right') ax4 = fig.add_subplot(gs[2, :2]) ax4.set_title('Bottom Left') ax5 = fig.add_subplot(gs[2, 2]) ax5.set_title('Bottom Right') plt.show()This code creates a Figure object with dimensions of 8x6 inches. Then, it creates a grid with three rows and three columns, where the width of each column increases from left to right and the height of each row decreases from top to bottom. The code then adds five subplots to the grid, using the `add_subplot` method of the Figure object. In summary, the add_gridspec function in the Matplotlib package library allows you to create grids of subplots within a Figure object. By specifying the number of rows and columns in the grid, as well as the height and width of each cell, you can create complex visualizations with multiple plots.