5
Lesson 5
Multiple Plots and subplots
You can plot multiple lines on the same plot by simply calling the plot() function multiple times before displaying the figure. Each call can have different datasets and customizations, allowing for comparative analysis within a single visualization.
Example:
Example:
import matplotlib.pyplot as plt # Sample data x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] # Quadratic y2 = [2, 3, 5, 7, 11] # Prime numbers # Create a line plot plt.plot(x, y1, label='Quadratic', color='blue', linestyle='-') plt.plot(x, y2, label='Prime', color='red', linestyle='--') # Add title and labels plt.title('Multiple Lines on Same Plot') plt.xlabel('X-axis') plt.ylabel('Y-axis') # Add legend plt.legend() # Show the plot plt.show()
In this example:
- Two lines are plotted on the same axes: one for quadratic values (y1) and another for prime numbers (y2).
- A legend is added to differentiate between the two lines.
Using subplot() to create multiple plots in one figure
The subplot() function allows you to create a grid of subplots within a single figure. You can specify the number of rows and columns in the grid, as well as the index of the current subplot.
Syntax:
plt.subplot(nrows, ncols, index)
- nrows: Number of rows of subplots.
- ncols: Number of columns of subplots.
- index: Index of the current subplot (starting from 1).
Example:
import matplotlib.pyplot as plt # Sample data for two plots x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] # Quadratic y2 = [2, 3, 5, 7, 11] # Prime numbers # Create a figure plt.figure(figsize=(10, 6)) # First subplot plt.subplot(2, 1, 1) # 2 rows, 1 column, 1st subplot plt.plot(x, y1, label='Quadratic', color='blue') plt.title('Quadratic Function') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() # Second subplot plt.subplot(2, 1, 2) # 2 rows, 1 column, 2nd subplot plt.plot(x, y2, label='Prime Numbers', color='red') plt.title('Prime Numbers') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.legend() # Adjust layout plt.tight_layout() # Show the plots plt.show()
In this example:
- Two subplots are created: one for the quadratic function and another for prime numbers.
- The tight_layout() function is used to adjust the spacing between the plots for better visual clarity.
3. Exercises:
Exercise: 1
1. Create multiple lines on one plot. Use the following data:
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10] (Linear)
y2 = [1, 3, 5, 7, 9] (Odd numbers)
2. Plot both lines on the same graph with appropriate labels, a title, and a legend.
Exercise: 2
1. Create a 2x2 grid of subplots using the following data:
a. First subplot: Linear function (y1)
b. Second subplot: Quadratic function (y2)
c. Third subplot: Cubic function (y3 = [1, 8, 27, 64, 125])
d. Fourth subplot: Prime numbers (y2)
2. Add titles and labels for each subplot.
Exercise: 3
1. Create a single figure with one main plot showing both y1 and y2 from Exercise 1.
2. Below that, create two subplots: one for the quadratic function and one for the cubic function from Exercise 2.
3. Ensure that all plots have appropriate titles and labels.
Conclusion
In this lesson, we learned how to plot multiple lines on the same graph for comparative analysis and how to create multiple subplots within a single figure using the