Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Create an Area Chart in Seaborn?
An Area Chart visualizes the cumulative magnitude of different variables over time or any ordered dimension. It displays data as stacked areas where each layer represents a variable, making it easy to compare the relative contributions of each variable to the total magnitude at any given point.
While Seaborn doesn't have a dedicated area chart function, we can create area charts using matplotlib.pyplot.stackplot() combined with Seaborn's styling capabilities for enhanced visual appeal.
Syntax
The basic syntax for creating an area chart with Seaborn styling ?
import seaborn as sns import matplotlib.pyplot as plt sns.set_theme() # Apply Seaborn styling plt.stackplot(x_data, y1_data, y2_data, y3_data, labels=['Series 1', 'Series 2', 'Series 3']) plt.show()
Basic Area Chart
Here's a simple example creating an area chart showing team performance over time ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Set seaborn style
sns.set_theme()
# Define DataFrame
df = pd.DataFrame({'period': [1, 2, 3, 4, 5, 6, 7, 8],
'team_A': [20, 12, 15, 14, 19, 23, 25, 29],
'team_B': [5, 7, 7, 9, 12, 9, 9, 4],
'team_C': [11, 8, 10, 6, 6, 5, 9, 12]})
# Create area chart
plt.stackplot(df.period, df.team_A, df.team_B, df.team_C)
plt.title('Team Performance Over Time')
plt.show()
Display: Area chart showing three stacked areas representing team performance across 8 periods
Customized Area Chart with Legend
This example shows how to add colors, labels, and legends to make the chart more informative ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Set seaborn style
sns.set_theme()
# Define DataFrame
df = pd.DataFrame({'period': [1, 2, 3, 4, 5, 6, 7, 8],
'team_A': [20, 12, 15, 14, 19, 23, 25, 29],
'team_B': [5, 7, 7, 9, 12, 9, 9, 4],
'team_C': [11, 8, 10, 6, 6, 5, 9, 12]})
# Define colors to use in chart
color_map = ['red', 'steelblue', 'pink']
# Create area chart with customizations
plt.stackplot(df.period, df.team_A, df.team_B, df.team_C,
labels=['Team A', 'Team B', 'Team C'],
colors=color_map)
# Add legend and labels
plt.legend(loc='upper left')
plt.xlabel('Period')
plt.ylabel('Points Scored')
plt.title('Team Performance Comparison')
# Display area chart
plt.show()
Display: Customized area chart with legend, axis labels, and custom colors showing team performance comparison
Common Customizations
| Parameter | Purpose | Example |
|---|---|---|
colors |
Set custom colors | colors=['red', 'blue', 'green'] |
labels |
Add series labels | labels=['Series 1', 'Series 2'] |
alpha |
Set transparency | alpha=0.7 |
baseline |
Set baseline position | baseline='zero' |
Key Points
Seaborn Styling Use
sns.set_theme()to apply Seaborn's aesthetic improvements to matplotlib plotsData Structure Organize data in separate arrays or DataFrame columns for each series
Stacking Order The order of data passed to
stackplot()determines the stacking orderLegends Always add legends when displaying multiple series for clarity
Conclusion
Area charts effectively visualize cumulative data and comparisons between multiple categories over time. By combining matplotlib.pyplot.stackplot() with Seaborn's styling, you can create professional-looking area charts that clearly communicate your data insights.
