9
Lesson 9
Stacked and filled area plots
Objective
By the end of this lesson, students will learn how to create stacked line plots and filled area plots using Matplotlib, and understand how to use the fill_between() function to enhance data visualization.
1. Creating stacked line plots:
Stacked line plots are useful for visualizing the cumulative total of different datasets over time. Each line in the plot is layered on top of the previous one, showing the overall trend.
Example: stacked line plot
By the end of this lesson, students will learn how to create stacked line plots and filled area plots using Matplotlib, and understand how to use the fill_between() function to enhance data visualization.
1. Creating stacked line plots:
Stacked line plots are useful for visualizing the cumulative total of different datasets over time. Each line in the plot is layered on top of the previous one, showing the overall trend.
Example: stacked line plot
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.arange(1, 6) # X-axis values y1 = [1, 2, 3, 4, 5] # First dataset y2 = [2, 3, 4, 5, 6] # Second dataset y3 = [1, 1, 1, 1, 1] # Third dataset # Create a stacked line plot plt.plot(x, y1, label='Dataset 1', marker='o') plt.plot(x, y2, label='Dataset 2', marker='o') plt.plot(x, np.array(y1) + np.array(y2) + np.array(y3), label='Cumulative', marker='o') # Add title and labels plt.title('Stacked Line Plot Example') plt.xlabel('X-axis') plt.ylabel('Values') plt.legend() # Show the plot plt.show()
In this example:
- Each dataset is plotted as a separate line.
- The third line represents the cumulative total of the first two datasets.
2. Creating filled area plots with fill_between():
Filled area plots display the area between a line and the x-axis (or between two lines) to emphasize the volume of data.
Syntax:
plt.fill_between(x, y1, y2, where, color, alpha)
- x : The x-coordinates.
- y1 : The y-values for the lower boundary.
- y2 : The y-values for the upper boundary (optional).
- where : A boolean array indicating where to fill.
- color : The color of the filled area (optional).
- alpha : The transparency level of the fill (optional).
Example: Filled area plot
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.arange(1, 6) y1 = [1, 3, 2, 5, 4] y2 = [2, 4, 3, 6, 5] # Create a filled area plot plt.fill_between(x, y1, y2, color='skyblue', alpha=0.4) # Add title and labels plt.title('Filled Area Plot Example') plt.xlabel('X-axis') plt.ylabel('Values') # Show the plot plt.show()
In this example:
- The area between the two lines is filled with a light blue color.
- The alpha parameter controls the transparency of the fill.
3. Customizing stacked and filled area plots:
You can customize the appearance of stacked and filled area plots in several ways, including colors, labels, and additional fills.
a. Customizing colors and transparency:
You can change the fill color and adjust the transparency to improve visual clarity.
Example:
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.arange(1, 6) y1 = [1, 2, 3, 4, 5] y2 = [2, 3, 4, 5, 6] # Create a filled area plot plt.fill_between(x, y1, color='orange', alpha=0.5, label='Dataset 1') plt.fill_between(x, y2, color='green', alpha=0.3, label='Dataset 2') # Add title and labels plt.title('Customized Filled Area Plot') plt.xlabel('X-axis') plt.ylabel('Values') plt.legend() # Show the plot plt.show()
In this example, different colors and transparency levels are used to represent each dataset distinctly.
b. Creating stacked filled area plots:
You can combine stacked line plots and filled area plots to create a more informative visualization.
Example:
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.arange(1, 6) y1 = [1, 2, 3, 4, 5] y2 = [2, 3, 4, 5, 6] # Create a filled area plot with stacked lines plt.fill_between(x, y1, color='orange', alpha=0.5, label='Dataset 1') plt.fill_between(x, y2, y1, color='lightgreen', alpha=0.5, label='Dataset 2') # Add title and labels plt.title('Stacked Filled Area Plot Example') plt.xlabel('X-axis') plt.ylabel('Values') plt.legend() # Show the plot plt.show()
In this example, one area is filled above the first dataset and another area is filled between the two datasets, creating a clear visual representation of both datasets.
4. Exercises:
Exercice 1:
1. Create a basic stacked line plot. Use the following data:
X-axis data: [1, 2, 3, 4, 5]
Dataset 1: [3, 4, 2, 5, 6]
Dataset 2: [1, 2, 3, 4, 3]
2. Plot both datasets and add a cumulative line.
Exercice 2:
1. Create a filled area plot. Use the following data:
X-axis data: [1, 2, 3, 4, 5]
Y-values for the first area: [1, 3, 2, 4, 5]
Y-values for the second area: [2, 4, 3, 5, 6]
2. Use the fill_between() function to create the filled area.
Exercice 3:
1. Create a plot using:
X-axis data: [1, 2, 3, 4, 5]
Dataset 1: [2, 3, 4, 5, 6]
Dataset 2: [1, 2, 1, 3, 2]
2. Fill the areas with different colors and adjust the transparency.
Conclusion
In this lesson, we explored how to create stacked line plots and filled area plots using Matplotlib. We learned how to customize the appearance of these plots to enhance visual clarity and convey more information. In the next lesson, we will delve into additional techniques for visualizing data in different formats using Matplotlib.