· Visualisations · 4 min read
Creating Stacked Bar Charts with Seaborn in Python
Creating Stacked Bar Charts with Seaborn in Python
Introduction
Stacked bar charts, used for visualizing relationships between different data variables, can be a valuable tool for developers and data analysts. They provide an easy way to represent categorical data, allowing you to assess various categories’ relative contributions to the whole. In this article, we’ll walk you through creating stacked bar charts using the powerful Seaborn data visualization library in Python.
Properties, Parameters, and Types
Seaborn offers a variety of chart types, including bar charts, but it doesn’t have a built-in function for stacked bar charts. However, with slight modifications to the common barplot
function in Seaborn, it’s possible to create stacked bar charts. Here are some essential properties, parameters, and types when working with Seaborn’s barplot
:
data
: The input dataset (usually a DataFrame) containing the relevant numerical values for the stacked chart.x
: Set the categorical variable on the x-axis.y
: Set the numerical variable on the y-axis.hue
: This parameter distinguishes between different categories in the chart.palette
: Customize the color palette of your chart.order
: Maintain the order of categories in your chart.dodge
: Prevent the bars from overlapping by setting this parameter toFalse
.
Adjusting these parameters accordingly will allow you to create a stacked bar chart using Seaborn.
Simplified Real-Life Example
Suppose you have a dataset showing the monthly sales of different products from an online store within the previous year. You can create a stacked bar chart to visualize the contribution of each product to the total sales.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Sample dataset
data = {'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
'Product A': [500, 600, 460, 700, 560, 480],
'Product B': [400, 700, 210, 490, 320, 620],
'Product C': [250, 360, 400, 320, 390, 545]}
df = pd.DataFrame(data)
# Melt the dataset into long format
df_melt = df.melt(id_vars=['Month'], var_name='Product', value_name='Sales')
# Create a stacked bar chart
sns.barplot(data=df_melt, x='Month', y='Sales', hue='Product', palette='viridis', dodge=False)
plt.title('Monthly Sales by Product')
plt.show()
In this example, we used the barplot
function combined with the necessary parameters to create a stacked bar chart. The data was first melted into a long-format DataFrame using the melt
function from pandas, which plays well with Seaborn’s barplot
.
More Complex Real-Life Example
Now, let’s assume you’re working with customer satisfaction data for an airline company. The data comprises the number of respondents ranking various aspects of the airline’s service (food quality, comfort, check-in experience, and in-flight entertainment) on a scale of 1 to 5, with 1 being the lowest rating and 5 being the highest.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
# Sample dataset
data = {'Rating': [1, 2, 3, 4, 5],
'Food Quality': [56, 120, 545, 1080, 1200],
'Comfort': [90, 140, 620, 1045, 1105],
'Check-in Experience': [45, 97, 605, 1000, 1253],
'In-Flight Entertainment': [28, 152, 530, 1170, 1120]}
df = pd.DataFrame(data)
# Melt dataset into long format
df_melt = df.melt(id_vars=['Rating'], var_name='Service Aspect', value_name='Number of Respondents')
# Create a stacked bar chart
sns.barplot(data=df_melt, x='Rating', y='Number of Respondents', hue='Service Aspect', palette='dark', dodge=False)
plt.title('Customer Satisfaction Ratings by Service Aspect')
plt.show()
Here, we have represented the customer satisfaction data using a more complex stacked bar chart that displays the number of respondents who ranked each service aspect at different rating levels.
Personal Tips
- Make sure your input dataset is well-structured in a format that Seaborn can work with. Long-format data using pandas’
melt
function can make it easier to create a stacked bar chart. - Customize your chart’s appearance using the
palette
parameter. Seaborn offers multiple color palettes that can improve your chart’s readability. - Always include a title and axis labels to make your chart more informative and easier to understand.
- Make use of Seaborn’s extensive documentation and examples to find ideas on customizing your stacked bar chart further.
- Once you’re familiar with creating stacked bar charts, explore other types of charts that Seaborn offers to diversify your data visualization toolkit.