Python Matplotlib Stackplot Example


Python Matplotlib Stackplot Example

Stack plots are a type of area plot that show how multiple data series contribute to a whole over time or another continuous variable. They are useful for visualizing cumulative trends.

In this tutorial, we will cover:

  • How to create a basic stack plot.
  • Customizing colors and labels for clarity.
  • Adding a legend and styling the plot.

Creating a Basic Stack Plot

A stack plot can be created using the stackplot() function in Matplotlib.

Example 1: Basic Stack Plot

import matplotlib.pyplot as plt

# Data for the stack plot
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 4, 5, 6]
y2 = [1, 2, 2, 3, 4]
y3 = [1, 1, 1, 2, 3]

# Create a stack plot
plt.stackplot(x, y1, y2, y3, labels=['Dataset 1', 'Dataset 2', 'Dataset 3'])

# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Stack Plot')

# Add a legend
plt.legend(loc='upper left')

# Show the plot
plt.show()

Explanation

  1. The stackplot() function is used to create the stack plot. The first argument is the x-axis data, followed by the y-axis data series.
  2. The labels parameter assigns labels to each data series for the legend.
  3. The legend() function adds a legend to the plot for better readability.
Basic Stack Plot

Customizing Stack Plot Colors and Labels

You can specify custom colors for each data series in a stack plot for better visualization.

Example 2: Stack Plot with Custom Colors

import matplotlib.pyplot as plt

# Data for the stack plot
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 4, 5, 6]
y2 = [1, 2, 2, 3, 4]
y3 = [1, 1, 1, 2, 3]

# Define custom colors
colors = ['skyblue', 'orange', 'lightgreen']

# Create a stack plot with custom colors
plt.stackplot(x, y1, y2, y3, colors=colors, labels=['Dataset 1', 'Dataset 2', 'Dataset 3'])

# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Stack Plot with Custom Colors')

# Add a legend
plt.legend(loc='upper left')

# Show the plot
plt.show()

Explanation

  1. The colors parameter is used to specify custom colors for each data series in the stack plot.
  2. Custom colors help to distinguish between the datasets more effectively.
Stack Plot with Custom Colors

Adding Style to the Stack Plot

You can further customize the stack plot by adding grid lines, adjusting font sizes, and positioning the legend outside the plot area.

Example 3: Styled Stack Plot

import matplotlib.pyplot as plt

# Data for the stack plot
x = [1, 2, 3, 4, 5]
y1 = [2, 3, 4, 5, 6]
y2 = [1, 2, 2, 3, 4]
y3 = [1, 1, 1, 2, 3]

# Define custom colors
colors = ['skyblue', 'orange', 'lightgreen']

# Create a stack plot
plt.stackplot(x, y1, y2, y3, colors=colors, labels=['Dataset 1', 'Dataset 2', 'Dataset 3'])

# Add labels and title
plt.xlabel('X-axis', fontsize=12)
plt.ylabel('Y-axis', fontsize=12)
plt.title('Styled Stack Plot', fontsize=14)

# Add grid lines
plt.grid(True, linestyle='--', alpha=0.7)

# Add a legend outside the plot
plt.legend(loc='upper center')

# Adjust layout
plt.tight_layout()

# Show the plot
plt.show()

Explanation

  1. The grid() function adds grid lines to the plot, improving visual alignment.
  2. The legend() function places the legend on the upper center area of plot.
  3. The tight_layout() function adjusts the spacing to ensure that all elements fit within the plot area.
Styled Stack Plot

Summary

In this tutorial, we explored:

  • How to create a basic stack plot using Matplotlib.
  • Customizing stack plot colors for better distinction between datasets.
  • Styling the stack plot with grid lines, legends, and layout adjustments.

Stack plots are a powerful way to visualize cumulative data trends over a continuous variable. By customizing colors, labels, and styles, you can create informative and visually appealing plots.




Python Libraries