Introduction
Have you ever looked at beautiful graphs in data science projects and wondered how do I create them myself? If yes, you're not alone and Seaborn is the perfect tool to help you get started.
Seaborn is a powerful Python library built over Matplotlib that makes it easier to create more informative, cleaner, and visually appealing statistical graphs. Whether you're presenting insights from a dataset, analyzing sales data, or visualizing customer behavior, Seaborn does it all with just a few lines of simple code.
In this easy-to-learn blog, I will walk you through all the important Seaborn charts with code, visuals, and when and where to use these graphs. By the end, you’ll be able to create stunning plots that make your data come alive.
Setting Up Your Environment
Before we dive into the graphs, let’s set up our environment:
# Install seaborn
pip install seaborn
Now, import the necessary libraries:
import seaborn as sns
import matplotlib.pyplot as plt
Loading the dataset:
# View available datasets
sns.get_dataset_names()
This piece of code will provide you with a list of all the datasets present.Some of the datasets present are iris,tips,penguins etc.
We’ll use the tips dataset, which contains information about restaurant tips.
# Load the dataset
df = sns.load_dataset('tips')
# View the first few rows
df.head()
This dataset includes features like total bill amount, tip amount, and customer info like sex, smoker, day, time, size, etc.
Types of Plots in Seaborn
After going through all these graphs, you'll be ready to make clean and stylish graphs in Seaborn.
Function-Level vs. Axes-Level Interface
The two primary plotting interfaces offered by Seaborn are Function-level and Axes-level.Although both are useful for efficiently visualizing data but their structure and degree of control vary.
Example:Lets say we have to plot a scatterplot so if you want to do it using:
Function-level interface: You simply use a generic function like sns.relplot() and specify the type of the plot using the kind parameter.This instructs Seaborn to use the relational plot function to produce a scatter plot
sns.relplot(data=tips, x='total_bill', y='tip', kind='scatter')
Axes-level interface: In this instance, you use the plotting function's name, sns.scatterplot(), to call it directly.This increases your control and makes it possible to integrate with the figure/axis objects in Matplotlib.
sns.scatterplot(data=tips, x="total_bill", y="tip")
Hence the function-level approach is quicker and simpler. Seaborn takes care of setting up the figure and axes, so you don't have to worry about it. Simply call a single function, such as relplot(), and use the kind parameter to specify the type of plot you want. This approach is excellent for beginners or for rapidly exploring your data.
You have more control with the axes-level approach. Before calling the particular Seaborn function, such as scatterplot() or boxplot(), you first create the figure and axes using Matplotlib. Although it requires a little more effort, this is very helpful when you want to alter the layout and design or create multiple plots in a single figure.
So, if you want something quick and simple, use the function-level interface. But if you need more flexibility and want to create detailed or customized plots, the axes-level method is better.
Now that we’ve understood how Seaborn’s interfaces work, let’s explore some of the most commonly used plots. These will help you visualize different types of data — from comparisons to distributions and relationships.
1. Relational Plots
It is used in Seaborn to visualize relationships between two variables i.e. it is used for Bivariate Analysis.The plots under this section are scatter plot and lineplot.
a.(i) Scatter Plot
It is used to visualize the relationship between two numerical variables.
sns.scatterplot(data=tips, x='total_bill', y='tip', hue='sex', style='time', size='size')
Output:
This axes level scatter plot code contains-
data=tips – We specify the dataset we are gonna be using.
x='total_bill' – The values on the x-axis represent the total bill.
y='tip' – The values on the y-axis represent the tip amount.
hue='sex' – Points are colored based on the gender (male/female) of the customer.
style='time' – The shape of each point depends on whether it was lunch or dinner.
size='size' – The size of every point represents the number of individuals at the table.
(ii) Scatter Plot (Function-level)
sns.relplot(data=tips, x='total_bill', y='tip', kind='scatter', hue='sex', style='time', size='size')
If you notice the difference between these two graphs the function level gives us a kind of a rectangle shaped graph whereas in axes level is square shaped and the details of the graph have shifted to the right.The code is the same just you mention the type of plot and in the kind property you give the name of the graph.You have more control in axes level graphs and hence we will be continuing with it.
b. Line Plot
A line plot is employed in illustrating the relationship between two variables, particularly when one variable signifies a sequence such as time, order, or continuous values. It is used in Trend Analysis,Comparisons,Patterns to identify growth, decline, spikes, or seasonality in data and for Smooth Transitions.
sns.lineplot(data=tips, x="size", y="total_bill")
Output:
2. Distribution Plots
A distribution plot allows you to visualize how your data is distributed. It displays the frequency of values as a histogram, and sometimes includes a smoothed curve to indicate overall shape. This is helpful for identifying patterns, such as whether your data is normally shaped or skewed.The plots under this section are histplot,kdeplot and rugplot.
a. Hist Plot
Shows the distribution of a single numeric variable.Useful for understanding how values are spread out.
sns.histplot(data=tips, x="total_bill", bins=10)
Output:
This piece of code employs Seaborn's histplot() to plot a histogram of the distribution of the total_bill column of the tips dataset.
Data=tips indicates which dataset to use
x="total_bill" indicates the column you would like to graph on the x-axis.
The bins=10 argument indicates that the total bill values will be split into 10 ranges (or bins), and the graph will indicate how many entries are in each range. It serves to enlighten you as to how restaurant bills are distributed — whether most of them are small, high, or somewhere in between.
b. KDE Plot
A KDE plot draws a smooth curve to show how your data is spread out. It’s like a smoother version of a histogram and helps you see the overall shape or pattern in the data more clearly.
sns.kdeplot(data=tips, x="total_bill")
c. ECDF Plot
An ECDF plot shows how the data accumulates over values. It helps you understand what proportion of values are less than or equal to a certain amount. It is useful for spotting medians, percentiles, and spread.
sns.ecdfplot(data=tips, x="total_bill")
Output:
3. Categorical Plots
They are used when you want to compare values that fall into distinct categories, such as gender, day of week, or product category. These plots show how data is binned together and what each bin does. Seaborn makes it really easy to plot categories like bar plots, box plots, violin plots, and more with functions like catplot(). These are especially useful when working with survey data, customer segments, or anything that falls into distinct groups.The types of plots in this category are barplot,boxplot,violinplot,countplot and swarmplot.
a. Bar Plot
They are used to show average (or other statistics) of a numerical value across categories.You must give both x and y values (category and numeric data).It calculates and plots the mean of the y values for each category in x.
sns.barplot(data=tips, x='day', y='total_bill')
Output:
b. Count Plot
It is used to count how many times each category appears in your data.You only need to give the x (or y) value — just the category.It’s like a bar chart of value counts (similar to value_counts() in pandas).
sns.countplot(data=tips, x='day')
Output:
c. Box Plot
A box plot summarizes the distribution of a numeric variable using five values: minimum, lower quartile (25%), median (50%), upper quartile (75%), and maximum. It also highlights outliers — data points that are unusually high or low.
sns.boxplot(data=tips, x='day', y='total_bill')
Output:
x='day', y='total_bill': For each day, Seaborn shows how the total bills are distributed.
You can clearly see the median line inside the box, the box itself (middle 50% of data), and points outside (outliers).
It’s a powerful tool to detect variation and inconsistency in your data.
d. Violin Plot
The violin plot builds on the box plot and adds a KDE (Kernel Density Estimate) on both sides to show the distribution shape of the data.While a box plot shows the summary, a violin plot shows where the data is concentrated.Each “violin” shows the density of data — wider sections mean more data points. You also get a central line and mini box inside that shows quartiles like a box plot.Great when you want both summary and distribution together.
sns.violinplot(data=tips, x='day', y='total_bill')
Output:
e. Swarm Plot
A swarm plot is like a smarter strip plot ,it adjusts the positions of points so that they don’t overlap and all are visible.It shows each data point clearly, while also giving a good sense of density (how many points are close together) and variation.
sns.swarmplot(data=tips, x='day', y='total_bill')
Output:
4. Matrix Plots
Matrix plot is a chart for visualizing relationships between two numerical variables or showing how multiple variables relate to each other across matrix/grid layout.These diagrams are especially beneficial for Correlation analysis and Pairwise relationships.This type consist of heatmap and clustermap.
a. Heatmap
-The heatmap derives its name from the concept that it is an affinity based fusion of a correlation matrix and the original visibility data. Each cell of the matrix is the pairing of two variables. Darker/lighter hues correspond to higher/low strength of association.
corr = tips.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()
Output:
b. Clustermap
A clustermap is similar to a heatmap, but in addition, it uses clustering algorithms (such as hierarchical clustering) to cluster together similar variables.It reorders rows and columns so that highly correlated variables are positioned close to one another.
corr = tips.corr(numeric_only=True)
sns.clustermap(corr, annot=True, cmap='viridis', figsize=(6, 4))
plt.show()
Output:
Variables that are more correlated (such as total_bill and tip) will be clustered closer on the axes. It helps you identify patterns and automatically arrange your data visually.
sns.clustermap(corr, annot=True, cmap='viridis'): Works just like a heatmap but adds dendrograms (tree diagrams) to show clustering.
The color bar still shows the correlation values.
Great when your dataset has many variables.
figsize=(6, 4) makes the figure 6 inches wide and 4 inches tall.
5. MultiPlots
Plotting several charts in a single figure or layout is known as a multiplot in data visualization. This is very helpful in situations when you wish to compare various visualizations side by side,The same data is being examined from several angles and for dashboards or reports.
a. FacetGrid
The Seaborn library includes the FacetGrid class which helps you display data through multiple plot grids. Through its functionality you can generate multiple plots that organize data according to category levels which reveals variable interactions and condition-based variations.
g = sns.FacetGrid(tips, col="sex", row="time")
g.map(sns.scatterplot, "total_bill", "tip")
plt.show()
Output:
b. Catplot
It is a higher-level function built on FacetGrid that lets you create categorical plots in grids quickly.
sns.catplot(data=tips, x="day", y="total_bill", kind="box", col="sex")
plt.show()
Output:
c. Pairplot
It creates a matrix of scatter plots for all combinations of numerical variables. It's not a FacetGrid but still shows multiple plots in one figure.
sns.pairplot(tips, hue="sex", height=2, aspect=1)
plt.show()
Output:
🎉 Conclusion
Thanks so much for stopping by and reading this blog! I hope it gave you a good and straightforward introduction to Seaborn and how to employ its various types of plots and I have only made graphs using axes level you can visit my github repo to explore function level too.
If you're brand new to data visualization or want to enhance your skills, I’m happy you came along for the ride! If you liked this, please do share it, and look out for more beginner-friendly guides like this and if you have any doubts feel free to message me.
🔗 View the complete code on GitHub: github.com/Aaryan-Lunis
Happy plotting! 📊
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.