~ 4 min read

Creating Stacked Area Charts with Seaborn in Python

By: Adam Richardson
Share:

Introduction to Stacked Area Charts with Seaborn

A stacked area chart is a visualization technique that illustrates how the values of multiple categories or variables change over time. The stacked area chart is particularly useful when comparing multiple variables to understand their relative significance within a specific context.

Seaborn is a popular data visualization library in Python that provides a high-level interface for creating informative and engaging statistical graphics. This article will guide you through the process of creating stacked area charts with Seaborn and Python to enhance your data analysis skills.

Properties and Parameters of Stacked Area Charts

The key components of a stacked area chart involve plotting multiple variables against a common axis, usually time. Each variable has a unique color or pattern, and the areas between the variables are filled to show the cumulative effect of the combined variables.

Some of the essential parameters to consider when creating a stacked area chart include:

  • x: The independent variable, usually time
  • y: The dependent variables, which represent the categories or variables of interest
  • data: The DataFrame containing the data to be plotted
  • hue: The column name in the data that defines the categories
  • palette: A dictionary that maps the categories to their corresponding colors

When selecting the right parameters for your chart, it’s crucial to consider the data structure, the relationships among the variables, and the story you’d like to tell with your visualization.

Simplified Real-life Example

Let’s create a stacked area chart using Seaborn to illustrate the sales of three products over a period of 12 months. First, we’ll import the necessary libraries and create a sample dataset:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {'Month': list(range(1, 13)),
        'Product_A': [50, 80, 120, 160, 210, 250, 290, 320, 370, 410, 480, 550],
        'Product_B': [30, 45, 90, 130, 145, 200, 280, 330, 375, 410, 460, 520],
        'Product_C': [20, 60, 100, 140, 190, 240, 250, 310, 340, 420, 480, 523]}
sales_data = pd.DataFrame(data)

Now that we have a sample dataset, let’s create a stacked area chart using Seaborn’s lineplot function:

plt.figure(figsize=(10, 6))
sns.lineplot(x="Month", y="Product_A", data=sales_data, label="Product A", ci=None)
sns.lineplot(x="Month", y="Product_B", data=sales_data, label="Product B", ci=None)
sns.lineplot(x="Month", y="Product_C", data=sales_data, label="Product C", ci=None)

plt.fill_between(sales_data['Month'], sales_data['Product_A'], color="b", alpha=0.3)
plt.fill_between(sales_data['Month'], sales_data['Product_A'], sales_data['Product_A'] + sales_data['Product_B'], color="g", alpha=0.3)
plt.fill_between(sales_data['Month'], sales_data['Product_A'] + sales_data['Product_B'], sales_data['Product_A'] + sales_data['Product_B'] + sales_data['Product_C'], color="r", alpha=0.3)

plt.title("Stacked Area Chart of Product Sales")
plt.xlabel("Month")
plt.ylabel("Sales")
plt.legend()
plt.show()

This code creates a stacked area chart with months on the x-axis and sales values on the y-axis. The areas between the lines are filled with blue, green, and red colors corresponding to the sales of Product A, Product B, and Product C, respectively.

Complex Real-life Example

Now let’s use a real-life dataset that contains information about monthly precipitation levels in different US cities. The dataset is available on GitHub and can be loaded directly using Pandas:

url = "https://raw.githubusercontent.com/JacobAlander/Data-4/main/precipitation.csv"
precipitation_data = pd.read_csv(url)
precipitation_data['Month'] = pd.to_datetime(precipitation_data['Month'], format='%Y-%m')
precipitation_data.set_index('Month', inplace=True)

Creating a stacked area chart for this dataset requires aggregating the precipitation levels by year and city. We can use the Pandas groupby function:

yearly_precipitation = precipitation_data.groupby([precipitation_data.index.year, 'City']).agg('sum').reset_index()
yearly_precipitation.rename(columns={'level_0': 'Year'}, inplace=True)

Finally, create the stacked area chart using Seaborn’s lineplot function and fill_between method:

plt.figure(figsize=(14, 8))

cities = yearly_precipitation['City'].unique()
palette = sns.color_palette("viridis", len(cities))

for index, city in enumerate(cities):
    city_data = yearly_precipitation[yearly_precipitation['City'] == city]
    sns.lineplot(x="Year", y="Precipitation", data=city_data, label=city, ci=None, color=palette[index])

stack_bottom = [0] * len(yearly_precipitation['Year'].unique())

for index, city in enumerate(cities):
    city_data = yearly_precipitation[yearly_precipitation['City'] == city].sort_values('Year')['Precipitation']
    plt.fill_between(yearly_precipitation['Year'].unique(), stack_bottom, stack_bottom + city_data, color=palette[index], alpha=0.6)
    stack_bottom = stack_bottom + city_data

plt.title("Stacked Area Chart of Yearly Precipitation by City")
plt.xlabel("Year")
plt.ylabel("Precipitation (inches)")
plt.legend()
plt.show()

This code creates a beautiful and informative stacked area chart displaying the yearly precipitation levels for each city.

Personal Tips

  • When working with real-life datasets, be sure to preprocess the data (cleaning, aggregating, or pivoting) to make it suitable for a stacked area chart.
  • Consider the color palette when creating the chart, as it can significantly impact the visual appeal and readability of the chart.
  • Always label your axes and provide a legend for readers to understand the chart with ease.
  • Use a suitable figure size to ensure all data points are visible and well-spaced.
  • Finally, thoroughly analyze and interpret the chart to extract insights and make data-driven decisions.
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts