WikiGalaxy

Personalize

Creating Subplots with plt.subplots()

The plt.subplots() function in Matplotlib is a versatile tool for creating multiple plots within a single figure. It provides a convenient way to generate subplots, allowing you to organize and display multiple graphs simultaneously. This function returns a tuple containing a figure object and an array of axes objects, which can be used to plot data on individual subplots.

Basic Subplots

In this example, we'll create a basic 2x2 grid of subplots using plt.subplots(). Each subplot will display a simple line plot.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [1, 2, 3])
axs[1, 0].plot([1, 2, 3], [3, 2, 1])
axs[1, 1].plot([1, 2, 3], [9, 4, 1])

plt.show()
    

This code creates a 2x2 grid of subplots, each displaying a different line plot. The axs array allows access to individual subplots for plotting.

Sharing Axes

Sharing axes between subplots can be useful for comparing data. Here, we'll create subplots that share the x-axis.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 1, sharex=True)
axs[0].plot([1, 2, 3], [1, 4, 9])
axs[1].plot([1, 2, 3], [1, 2, 3])

plt.show()
    

By setting sharex=True, the subplots share the same x-axis, making it easier to compare data across different plots.

Adjusting Spacing

Adjusting the spacing between subplots can improve the layout of your figure. Here, we'll use plt.subplots_adjust() to modify the spacing.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)
fig.subplots_adjust(hspace=0.5, wspace=0.3)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [1, 2, 3])
axs[1, 0].plot([1, 2, 3], [3, 2, 1])
axs[1, 1].plot([1, 2, 3], [9, 4, 1])

plt.show()
    

Using plt.subplots_adjust(), we can control the horizontal (wspace) and vertical (hspace) spacing between subplots for better visualization.

Different Plot Types

You can use different plot types within subplots to compare various data representations. Here, we'll mix line plots and scatter plots.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].scatter([1, 2, 3], [1, 2, 3])
axs[1, 0].plot([1, 2, 3], [3, 2, 1])
axs[1, 1].scatter([1, 2, 3], [9, 4, 1])

plt.show()
    

This example demonstrates how to use different plot types within subplots, providing a diverse view of data.

Customizing Subplot Appearance

Customization options allow you to personalize the appearance of subplots. Here, we'll adjust titles, labels, and colors.


import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2, 3], [1, 4, 9], color='red')
axs[0, 0].set_title('Quadratic')
axs[0, 1].plot([1, 2, 3], [1, 2, 3], color='green')
axs[0, 1].set_title('Linear')
axs[1, 0].plot([1, 2, 3], [3, 2, 1], color='blue')
axs[1, 0].set_title('Inverse')
axs[1, 1].plot([1, 2, 3], [9, 4, 1], color='purple')
axs[1, 1].set_title('Cubic')

plt.show()
    

This example illustrates how to customize subplots by setting titles, labels, and colors to enhance the presentation of data.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025