Saltar navegación

1.3.4 Creating Subplots and Layouts

Información

In this page, you will find the content of the section in both video and text formats. Videos are interactive and contain embedded content (explanations, links or exercises) throughout their playback.

At the end of this page, you have a link to the Jupyter/Colab notebook where you can practice the theory from this section.

Vídeo

Creating Subplots and Layouts

Welcome back to our Matplotlib course module. In this section, we will explore how to create subplots and arrange multiple plots within a single figure. Subplots are useful for comparing different datasets or visualizations side by side. Understanding how to create and customize subplots will allow you to present your data more effectively. Let's get started!

Creating Subplots

Subplots are multiple plots arranged within a single figure. You can create subplots using the plt.subplot function or the plt.subplots function. The plt.subplots function is more flexible and is recommended for creating complex layouts.

Here's how you can create a basic 2x2 grid of subplots using plt.subplots:

import matplotlib.pyplot as plt

# Data for plotting
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
y2 = [2, 3, 5, 7, 11]
y3 = [1, 3, 6, 10, 15]
y4 = [1, 2, 1, 2, 1]

# Creating a 2x2 grid of subplots
fig, axs = plt.subplots(2, 2)

# Plotting in each subplot
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Plot 1')

axs[0, 1].scatter(x, y2)
axs[0, 1].set_title('Plot 2')

axs[1, 0].bar(x, y3)
axs[1, 0].set_title('Plot 3')

axs[1, 1].plot(x, y4)
axs[1, 1].set_title('Plot 4')

# Adjusting layout to prevent overlap
plt.tight_layout()

# Displaying the plots
plt.show()

In this example, we create a 2x2 grid of subplots using plt.subplots(2, 2). We then plot different types of plots in each subplot and add titles to each one. Finally, we use plt.tight_layout() to adjust the spacing between subplots to prevent overlap.

Customizing Subplots

You can customize subplots in various ways to make them more informative and visually appealing. This includes setting axis labels, adjusting the size of the figure, and modifying the spacing between subplots.

Here's how you can customize subplots:

# Creating a 2x2 grid of subplots with a larger figure size
fig, axs = plt.subplots(2, 2, figsize=(10, 8))

# Plotting in each subplot
axs[0, 0].plot(x, y)
axs[0, 0].set_title('Plot 1')
axs[0, 0].set_xlabel('X Axis')
axs[0, 0].set_ylabel('Y Axis')

axs[0, 1].scatter(x, y2, color='r')
axs[0, 1].set_title('Plot 2')
axs[0, 1].set_xlabel('X Axis')
axs[0, 1].set_ylabel('Y Axis')

axs[1, 0].bar(x, y3, color='g')
axs[1, 0].set_title('Plot 3')
axs[1, 0].set_xlabel('X Axis')
axs[1, 0].set_ylabel('Y Axis')

axs[1, 1].plot(x, y4, linestyle='--', marker='o')
axs[1, 1].set_title('Plot 4')
axs[1, 1].set_xlabel('X Axis')
axs[1, 1].set_ylabel('Y Axis')

# Adjusting layout to prevent overlap
plt.tight_layout()

# Displaying the plots
plt.show()

In this example, we customize the subplots by setting axis labels and changing the figure size using the figsize parameter. We also adjust the colors and line styles of the plots within each subplot.

Creating Complex Layouts

For more complex layouts, you can use GridSpec, which provides more control over the arrangement of subplots. GridSpec allows you to specify the relative size of rows and columns, merge cells, and create irregular layouts.

Here's an example of using GridSpec for a complex layout:

import matplotlib.gridspec as gridspec

# Creating a complex layout with GridSpec
fig = plt.figure(figsize=(10, 8))
gs = gridspec.GridSpec(3, 3)

# Creating subplots in the GridSpec layout
ax1 = fig.add_subplot(gs[0, :2])
ax2 = fig.add_subplot(gs[0, 2])
ax3 = fig.add_subplot(gs[1, :2])
ax4 = fig.add_subplot(gs[1:, 2])
ax5 = fig.add_subplot(gs[2, :2])

# Plotting in each subplot
ax1.plot(x, y)
ax1.set_title('Plot 1')

ax2.scatter(x, y2, color='r')
ax2.set_title('Plot 2')

ax3.bar(x, y3, color='g')
ax3.set_title('Plot 3')

ax4.plot(x, y4, linestyle='--', marker='o')
ax4.set_title('Plot 4')

ax5.plot(x, [i*2 for i in y], color='purple')
ax5.set_title('Plot 5')

# Adjusting layout to prevent overlap
plt.tight_layout()

# Displaying the plots
plt.show()

In this example, we use GridSpec to create a complex layout with irregular subplot sizes. We then plot different types of plots in each subplot and adjust the layout to prevent overlap.

Summary

In this section, we covered the basics of creating and customizing subplots using Matplotlib. You learned how to create a grid of subplots, customize them by setting axis labels and changing the figure size, and create complex layouts using GridSpec. These techniques allow you to present multiple visualizations side by side, making it easier to compare and analyze data.

In the next section, we will explore advanced data visualization techniques, including histograms, box plots, and pie charts. Stay tuned for more advanced plotting techniques!

Practice

Below, you have a link to the Jupyter/Colab notebook where you can practice the theory from this section:

Creating Subplots and Layouts

Feito con eXeLearning (Nova xanela)