~ 4 min read

Creating Area Charts with Seaborn in Python

By: Adam Richardson
Share:

Introduction to Area Charts with Seaborn

Area Charts are useful tools for visualizing data trends by stacking and displaying values over the x-axis. Using Seaborn, a powerful Python data visualization library, it becomes easier to create and customize these charts. In this article, we’ll explore how to create area charts with Seaborn and discuss the various parameters and properties that can be utilized to analyze and display data effectively.

Properties and Parameters of Area Charts in Seaborn

To create area charts using Seaborn, we leverage the lineplot function, which allows us to manipulate the following properties and parameters:

  • x and y: Data or names of variables in data.
  • data: A long-form pandas DataFrame.
  • palette: A seaborn color palette or a dictionary mapping hue levels to colors.
  • hue: The semantic variable that maps data to different colors.
  • style: The semantic variable that maps data to different line styles.
  • size: The semantic variable that maps data to different line widths.
  • sizes: List, dict, or tuple of sizes for the lines.
  • estimator: Callable that maps vector -> scalar, optional estimate to show.
  • ci: Size of the confidence interval to draw around estimated values. If “sd”, skip bootstrapping and draw the standard deviation of the observations.
  • n_boot: Number of bootstraps to use for computing the confidence interval.

Simple Example: Area Chart with Seaborn

Here is a simplified example of creating an area chart with Seaborn using a dataset representing sales data over different months in a year:

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

# Create sample dataset
data = {'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        'Sales_A': [1050, 1200, 1320, 1400, 1980, 2100, 1700, 1600, 1500, 1800, 1900, 2000],
        'Sales_B': [1100, 1300, 1400, 1500, 1650, 1850, 1900, 1600, 1400, 1550, 1650, 1750]}

df = pd.DataFrame(data)

# Melt data
df = pd.melt(df, id_vars=['Month'], var_name='Sales Person', value_name='Sales')

# Create area chart using Seaborn
sns.lineplot(x="Month", y="Sales", hue="Sales Person", data=df, estimator=np.sum, ci=None)
plt.fill_between(df["Month"].unique(), df.groupby(["Month"])["Sales"].transform(sum).unique(), alpha=0.3)

plt.title("Monthly Sales for Sales Person A and B")
plt.show()

This example creates an area chart representing the monthly sales for two salespersons. The semantically meaningful variable “Sales Person” is set as the hue parameter to differentiate the data using different colors.

Complex Example: Area Chart with Multiple Line Styles

Let’s consider a more complex example where we create an area chart for multiple stores’ sales data, and each store has data for several years.

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

# Sample data for 3 stores with sales data for 3 years
data = {'Year': [2018, 2019, 2020] * 3,
        'Store': ['Store A'] * 3 + ['Store B'] * 3 + ['Store C'] * 3,
        'Sales': [22000, 29000, 34000, 21000, 27000, 32000, 19000, 25000, 30000]}

df = pd.DataFrame(data)

# Area chart using Seaborn
sns.lineplot(x="Year", y="Sales", hue="Store", style="Store", data=df, estimator=np.sum, ci=None,
        palette=['green', 'blue', 'red'])
plt.gca().xaxis.set_major_locator(plt.MaxNLocator(integer=True))

# Calculating area fill coordinates
years = df["Year"].unique()
grouped_df = df.groupby("Year")["Sales"].sum()
for year, sales in zip(years, grouped_df):
    upper_val = sales - df[(df["Year"] == year) & (df["Store"] == 'Store A')]["Sales"].values[0]
    plt.fill_betweenx([0, upper_val], year - 0.5, year + 0.5, facecolor='green', alpha=0.3)

plt.title("Annual Sales for 3 Different Stores")
plt.show()

In this example, we create an area chart displaying sales data for three different stores over three years. We use the style and palette parameters for differentiation between the stores.

Personal Tips for Area Charts with Seaborn

  1. Always choose an appropriate color palette for your area chart to make it visually appealing and easy to read.
  2. Utilize the estimator and ci parameters effectively to create informative and insightful charts.
  3. Make use of style, size, and sizes parameters to differentiate between multiple data series when needed.
  4. Customize the area fills using matplotlib.pyplot functions to enhance the visual representation.
  5. Experiment with different Seaborn themes and context settings to produce eye-catching and professional-looking charts.
Subscribe to our newsletter

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

Related Posts