Menu

Data Visualization with Seaborn

Data Visualization with Seaborn

Seaborn is a library built on Matplotlib that makes attractive statistical charts with very little code. It works directly with Pandas DataFrames. This topic introduces its basic style and the main plots you will use every day.

a. Introduction to Seaborn

Seaborn is designed for statistical visualization. Instead of drawing each part of a chart by hand, you tell Seaborn which DataFrame and which columns to use, and it builds the chart for you. It also adds clean default styles.

Install it with pip install seaborn, and import it with the short name sns. Seaborn charts are Matplotlib objects underneath, so you can still use Matplotlib commands to adjust titles and labels.

import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
sns.barplot(data=tips, x="day", y="total_bill")
plt.show()

b. Seaborn Themes and Styles

Seaborn has built-in themes that change the look of every chart. Use sns.set_theme() to apply a clean default. You can choose styles such as darkgrid, whitegrid, dark, white, and ticks.

sns.set_theme(style="whitegrid", context="talk", palette="deep")

The context setting scales labels and lines for different uses. The options are paper, notebook, talk, and poster. Use talk when making charts for slides, since the text is larger.

Set the theme once at the top of your notebook so that every chart stays consistent. Consistency makes your work look professional.

c. Categorical Plots

Categorical plots compare values across groups. The bar plot shows the average of each group with a confidence interval. The count plot shows how many rows fall in each group.

sns.countplot(data=tips, x="day")
sns.boxplot(data=tips, x="day", y="total_bill")
sns.stripplot(data=tips, x="day", y="total_bill")

The strip plot and the swarm plot show every individual point, which helps when the dataset is small. The box plot and the violin plot summarize the distribution of each group.

You can combine them, such as placing a strip plot over a box plot, to show both the summary and the raw data.

d. Working with DataFrames in Seaborn

Seaborn works best with data in a tidy, or long, format. Each row is one observation and each column is one variable. If your data is wide, use melt() in Pandas to reshape it first.

You pass column names as text to x, y, and hue. The hue option splits the data by a category and colors each group differently.

sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time")

Seaborn automatically adds labels and a legend from your column names. This saves time and reduces errors. Keep your column names clear and readable.


Data Visualization Tutorial

WR

Wesline Rinisha



Learn via Video Course

Data Visualization Using Python Logo

Data Visualization Using Python

19 hrs



Get in Touch For Details