Stack Plots in Matplotlib¶
Stack plots, or area plots, are an excellent visualization tool for displaying cumulative data changes over time, making them useful for trend analysis.
Setting Up Stack Plots in Matplotlib¶
To start, let’s import the necessary libraries and configure a basic style for our plot.
import matplotlib.pyplot as plt
# Setting a style for aesthetics
plt.style.use('fivethirtyeight')
Explanation:
- Style: Using
fivethirtyeight
style gives our plot a clean, modern look. - Pyplot Import: Importing
matplotlib.pyplot
allows us to access essential plotting functions in Matplotlib.
Creating a Basic Stack Plot¶
A stack plot is effective for tracking cumulative totals and category breakdowns over time. Let’s say we want to track points scored by three players over several games.
Step-by-Step Code:
- Define the x-axis (time intervals): In this example, each
x
value could represent a different game. - Define the y-axis data: For each player, we’ll track the points they scored over the games.
# Data
games = [1, 2, 3, 4, 5, 6] # Time intervals (e.g., games)
player1_points = [5, 7, 6, 8, 7, 10]
player2_points = [3, 5, 4, 6, 4, 6]
player3_points = [2, 3, 4, 5, 6, 7]
# Creating the stack plot
plt.stackplot(games, player1_points, player2_points, player3_points)
plt.title("Points Scored by Players Over Time")
plt.xlabel("Games")
plt.ylabel("Points")
plt.show()
Explanation:
- x-axis (games): Defines each time point, such as games or years.
- y-axis values: Each list represents points scored by a different player across the games.
- stackplot() function: Creates an area plot stacking each player's points over time.
Adding Labels and Legends¶
Legends are essential to clarify which categories (or players) contribute to each section of the stack plot.
# Adding labels for each player
plt.stackplot(games, player1_points, player2_points, player3_points, labels=['Player 1', 'Player 2', 'Player 3'])
plt.title("Points Scored by Players Over Time")
plt.xlabel("Games")
plt.ylabel("Points")
plt.legend(loc='upper left')
plt.show()
Explanation:
- Labels: Adding labels for each player makes it clear which area represents each player.
- Legend Positioning: Using
loc='upper left'
places the legend in the upper left, where it does not interfere with the main plot area.
Customizing Stack Plot Colors¶
Customizing colors helps differentiate each category in the stack plot and provides a polished, professional look. You can use either named colors or hex color codes for more precision.
# Defining custom colors for each player
colors = ['#FF9999', '#66B3FF', '#99FF99'] # Light red, blue, and green
# Plotting with custom colors
plt.stackplot(games, player1_points, player2_points, player3_points, labels=['Player 1', 'Player 2', 'Player 3'], colors=colors)
plt.title("Points Scored by Players Over Time")
plt.xlabel("Games")
plt.ylabel("Points")
plt.legend(loc='upper left')
plt.show()
Explanation:
- Custom Colors: Specifying hex color codes for each player adds a more refined look to the stack plot.
- Colors Parameter: The
colors
parameter instackplot()
allows us to assign a specific color to each area, making it easier to distinguish between players or categories.