Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Matplotlib Subplot in Python

Matplotlib Subplot in Python

Matplotlib Subplots in Python

In Matplotlib, subplots allow you to create multiple plots in a single figure. This is useful when you want to display different plots side-by-side or in a grid layout within the same figure.

You can create subplots using the plt.subplots() function. This function returns a figure and an array of axes. You can then plot different data on each axis.


Basic Subplots Example

In the basic usage, you can specify how many rows and columns you want for your grid of subplots.

Example: Creating a 1x2 Grid of Subplots

import matplotlib.pyplot as plt# Data for plottingx = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]y2 = [25, 16, 9, 4, 1]# Create a 1x2 grid of subplots (1 row, 2 columns)fig, (ax1, ax2) = plt.subplots(1, 2)# Plot on the first subplotax1.plot(x, y1, 'r')ax1.set_title('Plot 1')# Plot on the second subplotax2.plot(x, y2, 'b')ax2.set_title('Plot 2')# Show the plotplt.show()

Explanation:

  • plt.subplots(1, 2): Creates a grid with 1 row and 2 columns. This returns a figure (fig) and an array of axes (ax1, ax2).

  • ax1.plot() and ax2.plot(): Plot the data on the respective subplots.

  • ax1.set_title() and ax2.set_title(): Set titles for the subplots.


Subplots with More Rows and Columns

You can create a more complex grid by specifying more rows and columns in the subplots() function.

Example: Creating a 2x2 Grid of Subplots

import matplotlib.pyplot as plt# Data for plottingx = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]y2 = [25, 16, 9, 4, 1]y3 = [2, 3, 5, 7, 11]y4 = [10, 8, 6, 4, 2]# Create a 2x2 grid of subplots (2 rows, 2 columns)fig, axes = plt.subplots(2, 2)# Plot on the first subplot (top-left)axes[0, 0].plot(x, y1, 'r')axes[0, 0].set_title('Plot 1')# Plot on the second subplot (top-right)axes[0, 1].plot(x, y2, 'g')axes[0, 1].set_title('Plot 2')# Plot on the third subplot (bottom-left)axes[1, 0].plot(x, y3, 'b')axes[1, 0].set_title('Plot 3')# Plot on the fourth subplot (bottom-right)axes[1, 1].plot(x, y4, 'purple')axes[1, 1].set_title('Plot 4')# Adjust layout for better spacingplt.tight_layout()# Show the plotplt.show()

Explanation:

  • plt.subplots(2, 2): Creates a grid of 2 rows and 2 columns.

  • axes[0, 0], axes[0, 1], etc.: Access individual subplots from the 2x2 grid.

  • plt.tight_layout(): Adjusts the subplot layout to prevent overlapping labels and titles.


Adjusting the Size of Subplots

You can change the size of the entire figure (and hence the subplots) using the figsize argument in plt.subplots().

Example: Adjusting the Figure Size

import matplotlib.pyplot as plt# Data for plottingx = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]y2 = [25, 16, 9, 4, 1]# Create a 1x2 grid of subplots with a custom figure sizefig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))# Plot on the first subplotax1.plot(x, y1, 'r')ax1.set_title('Plot 1')# Plot on the second subplotax2.plot(x, y2, 'b')ax2.set_title('Plot 2')# Show the plotplt.show()

Explanation:

  • figsize=(10, 5): Specifies the width and height of the figure in inches. In this case, the figure will be 10 inches wide and 5 inches tall.


Sharing Axes between Subplots

You can share axes between subplots by setting the sharex and sharey parameters to True. This is useful when you want to compare multiple plots with the same axis scales.

Example: Sharing X and Y Axes

import matplotlib.pyplot as plt# Data for plottingx = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]y2 = [25, 16, 9, 4, 1]# Create a 1x2 grid of subplots with shared x and y axesfig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)# Plot on the first subplotax1.plot(x, y1, 'r')ax1.set_title('Plot 1')# Plot on the second subplotax2.plot(x, y2, 'b')ax2.set_title('Plot 2')# Show the plotplt.show()

Explanation:

  • sharex=True: Ensures both subplots share the same x-axis.

  • sharey=True: Ensures both subplots share the same y-axis.


Adding Titles for All Subplots

If you want to add a title to the entire figure, you can use fig.suptitle().

Example: Adding a Title to the Entire Figure

import matplotlib.pyplot as plt# Data for plottingx = [1, 2, 3, 4, 5]y1 = [1, 4, 9, 16, 25]y2 = [25, 16, 9, 4, 1]# Create a 1x2 grid of subplotsfig, (ax1, ax2) = plt.subplots(1, 2)# Plot on the first subplotax1.plot(x, y1, 'r')ax1.set_title('Plot 1')# Plot on the second subplotax2.plot(x, y2, 'b')ax2.set_title('Plot 2')# Add a title for the entire figurefig.suptitle('Multiple Subplots Example')# Show the plotplt.show()

Explanation:

  • fig.suptitle('Multiple Subplots Example'): Adds a title to the entire figure, which is displayed above the subplots.


Conclusion

  • plt.subplots() is a versatile function that allows you to create multiple subplots in a single figure.

  • You can customize the grid layout, share axes, and adjust figure size.

  • fig.suptitle() adds a title to the whole figure, while individual axes can have their own titles.

Using subplots is a great way to compare different datasets or visualizations side by side without cluttering the figure.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql