Menu

Statistical Visualization with Seaborn

Statistical Visualization with Seaborn

Seaborn builds on Matplotlib to create clean, informative charts with very little code. It is ideal for exploring patterns in data.

a. Introduction to Seaborn

Seaborn is a Python library made for statistical charts. It works directly with Pandas DataFrames and gives you attractive default styles. You can make complex charts in a single line.

You import it with the short name sns. Use sns.set_theme() to apply a clean look to all your charts.

import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme()
sns.barplot(data=df, x="region", y="sales")
plt.show()

You pass the DataFrame with data and name the columns for x and y. Seaborn handles the rest, including colors and labels. It also builds on Matplotlib, so you can still use plt.title() and other commands to customize.

b. Distribution Plots and Box Plots

A distribution plot shows how values are spread. Use sns.histplot() to see a histogram, and add kde=True to include a smooth curve over the bars.

sns.histplot(data=df, x="salary", kde=True)
sns.boxplot(data=df, x="department", y="salary")

A box plot shows the median, the quartiles, and possible outliers for each group. The box covers the middle 50 percent of values. The dots outside the whiskers are potential outliers.

Use histograms to understand the shape of one column. Use box plots to compare a number across categories. Both are key tools in EDA.

c. Heatmaps and Pair Plots

A heatmap uses color to show values in a table. It is most often used to display a correlation table. Strong relationships stand out right away.

sns.heatmap(df.corr(numeric_only=True), annot=True, cmap="coolwarm")

The annot=True option writes the numbers inside each cell. A pair plot creates a grid of scatter plots for every pair of numeric columns, with histograms on the diagonal.

sns.pairplot(df[["age", "salary", "experience"]])

Pair plots give a fast overview of relationships in your data. Use them on a small set of columns, as a large number of columns makes the grid too crowded to read.

d. Customizing Seaborn Charts

Seaborn charts are easy to customize. Use the hue option to color points or bars by a category. This adds another layer of information without making a new chart.

sns.scatterplot(data=df, x="age", y="salary", hue="department")

You can change the size with plt.figure(figsize=(10, 6)) and set a color palette using the palette option. Add titles and axis labels with plt.title(), plt.xlabel(), and plt.ylabel().

Keep your charts simple. Use clear labels and a limited set of colors. To save a chart, use plt.savefig("chart.png", dpi=300) for a sharp image.


Data Analysis with Python Handbook

WR

Wesline Rinisha



Learn via Video Course

Data Analysis and Processing with Python using Pandas - Data Science Course Logo

Data Analysis and Processing with Python using Pandas - Data Science Course

5 hrs



Get in Touch For Details