Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

Matplotlib Subplot

To create multiple subplots in Matplotlib, you can use the `subplot()` function. This function allows you to specify the number of rows, the number of columns, and the index of the subplot you want to create.

WHEN TO USE SUBPLOT

 

Subplots in Matplotlib are useful in various scenarios, depending on the nature of your data and the visualization goals you want to achieve. Here are some situations where subplots can be beneficial:

1. Comparing Multiple Datasets: When you want to compare multiple datasets or variables side by side, subplots allow you to visualize them simultaneously. For example, you might want to compare the sales performance of different products, the temperature variations across different cities, or the stock prices of various companies.

2. Visualizing Different Aspects of Data: Subplots can be used to visualize different aspects or features of your data within the same figure. For instance, you might want to plot the time series data along with its corresponding trendline, seasonal components, and residuals in separate subplots for better analysis.

3. Faceting Categorical Data: If you have categorical variables in your dataset, subplots can be used to create facets or small multiples for each category. This allows you to examine the relationship or distribution of the data across different categories more effectively.

4. Comparing Model Outputs: When evaluating the performance of multiple models or algorithms, subplots can help you visualize their predictions, errors, or performance metrics side by side. This allows for a more comprehensive comparison and analysis of the models.

5. Creating Custom Layouts: Subplots provide flexibility in creating custom layouts for your visualizations. You can arrange the subplots in rows, columns, or grids based on your preference and the structure of your data. This enables you to design complex and informative visualizations tailored to your specific needs.

Overall, subplots are versatile tools that enhance the clarity and interpretability of your visualizations by organizing and presenting your data in a structured and meaningful way. They allow you to explore different aspects of your data simultaneously, facilitating a deeper understanding and analysis of the underlying patterns and relationships.

 

Here’s how you can create subplots in Matplotlib:

“`python
import matplotlib.pyplot as plt

# Create some sample data
x = [1, 2, 3, 4, 5]
y1 = [10, 15, 7, 10, 5]
y2 = [5, 8, 10, 15, 12]
y3 = [8, 6, 11, 9, 10]

# Create subplots
plt.figure(figsize=(10, 4)) # Set the size of the entire figure

# Subplot 1
plt.subplot(1, 3, 1) # 1 row, 3 columns, subplot 1
plt.plot(x, y1, color=’blue’)
plt.title(‘Subplot 1’)

# Subplot 2
plt.subplot(1, 3, 2) # 1 row, 3 columns, subplot 2
plt.plot(x, y2, color=’red’)
plt.title(‘Subplot 2’)

# Subplot 3
plt.subplot(1, 3, 3) # 1 row, 3 columns, subplot 3
plt.plot(x, y3, color=’green’)
plt.title(‘Subplot 3’)

# Adjust layout to prevent overlap
plt.tight_layout()

# Show plot
plt.show()
“`

In this example:
– We first import Matplotlib as `plt`.
– We create some sample data for three different datasets (`y1`, `y2`, `y3`).
– We create a figure using `plt.figure()` and specify the size of the entire figure using the `figsize` parameter.
– We create subplots using `plt.subplot()`. The first argument specifies the number of rows, the second argument specifies the number of columns, and the third argument specifies the index of the current subplot.
– Inside each subplot, we plot the corresponding data and set titles using `plt.plot()` and `plt.title()`.
– We adjust the layout to prevent overlap using `plt.tight_layout()`.
– Finally, we display the plot using `plt.show()`.

This code will create a figure with three subplots arranged horizontally. Each subplot will display a different dataset. You can adjust the number of rows, columns, and the size of the figure to customize the layout according to your needs.

 

Example 2:

“`python
import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 5, 7, 11]
y2 = [3, 5, 7, 9, 12]

# Create subplots
plt.subplot(2, 1, 1) # 2 rows, 1 column, subplot index 1
plt.plot(x, y1)
plt.title(‘Subplot 1’)

plt.subplot(2, 1, 2) # 2 rows, 1 column, subplot index 2
plt.plot(x, y2)
plt.title(‘Subplot 2’)

# Adjust layout to prevent overlap
plt.tight_layout()

# Show plot
plt.show()
“`

In this example:
– We import the `matplotlib.pyplot` module as `plt`.
– We define sample data `x`, `y1`, and `y2`.
– We create subplots using the `subplot()` function. The arguments `(2, 1, 1)` indicate that we want 2 rows, 1 column of subplots, and we’re creating subplot 1. Similarly, `(2, 1, 2)` creates subplot 2.
– We plot `y1` in the first subplot and `y2` in the second subplot.
– We add titles to each subplot using the `title()` function.
– We adjust the layout to prevent overlap between subplots using `plt.tight_layout()`.
– Finally, we display the plot using `plt.show()`.

You can create more complex layouts with multiple rows and columns of subplots by adjusting the arguments passed to `subplot()`. For example, `(2, 2, 1)` would create a grid of 2 rows and 2 columns with subplot index 1.

Join the conversation