DS Seaborn Visualization

Seaborn is a Python library built on top of Matplotlib. It produces polished statistical charts with short commands and works directly with Pandas tables. A chart that needs fifteen lines in Matplotlib often needs one line in Seaborn.

Install and Import

pip install seaborn

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_theme()
tips = sns.load_dataset("tips")
print(tips.head())

The tips dataset ships with Seaborn. It records restaurant bills, tips, days, and group sizes, which makes it a friendly practice set.

Pick the Right Chart

QuestionChartSeaborn Function
How are values spread?Histogramhistplot
How do groups compare?Box plotboxplot
Do two numbers move together?Scatter plotscatterplot
How many items per category?Bar countcountplot
Which columns relate to each other?Heatmapheatmap

Histogram: See the Spread

sns.histplot(data=tips, x="total_bill", bins=20, kde=True)
plt.show()

Bars count how many bills fall in each price range. The kde option adds a smooth curve over the bars.

Box Plot: Compare Groups

sns.boxplot(data=tips, x="day", y="total_bill")
plt.show()

How to read a box plot

        o  <-- outlier (unusually high bill)
        |
     +-----+  <-- top of box: 75% of bills fall below this
     |     |
     |-----|  <-- line inside: median bill
     |     |
     +-----+  <-- bottom of box: 25% of bills fall below this
        |
        |  <-- whisker: normal range continues

The box covers the middle half of the values. Short boxes show steady data, and tall boxes show wide variety.

Scatter Plot: Find Relationships

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

Each dot is one meal. The hue option colors dots by lunch or dinner. The size option makes dots larger for bigger groups. A cloud of dots rising from left to right shows that larger bills bring larger tips.

Count Plot: Count Categories

sns.countplot(data=tips, x="day", hue="sex")
plt.show()

Heatmap: Spot Strong Links

corr = tips[["total_bill", "tip", "size"]].corr()
sns.heatmap(corr, annot=True, cmap="Blues")
plt.show()

A heatmap colors each cell by the strength of the link between two columns. Numbers near 1 show a strong positive link. Numbers near 0 show no link. Numbers near negative 1 show an opposite link.

Pair Plot: Overview in One Image

sns.pairplot(tips, hue="time")
plt.show()

A pair plot draws a scatter plot for every pair of numeric columns, with histograms on the diagonal. Use it early in exploration to find promising relationships.

Style Your Charts

  • Call sns.set_theme(style="whitegrid") for a clean grid background.
  • Set figure size with plt.figure(figsize=(8, 5)).
  • Add titles with plt.title and axis labels with plt.xlabel and plt.ylabel.
  • Choose color palettes with the palette option, such as "viridis" or "Set2".

Accessible colors help every reader. Pick palettes that stay readable for people with color vision differences, and label categories directly where space allows.

Seaborn versus Matplotlib

FeatureMatplotlibSeaborn
Code lengthLongerShorter
Default lookPlainPolished
Statistical chartsManual workBuilt in
Fine controlFullUses Matplotlib underneath

Both libraries work together. Draw with Seaborn, and fine-tune with Matplotlib commands.

Mini Project: Explore Restaurant Tips

Combine the charts above to answer a real question: does group size change the tip percentage? Build a new column for tip percentage first, then draw a box plot by group size.

tips["tip_pct"] = tips["tip"] / tips["total_bill"] * 100
sns.boxplot(data=tips, x="size", y="tip_pct")
plt.title("Tip Percentage by Group Size")
plt.show()

Look at the median line in each box. Groups of one or two diners often show wider spreads, and large groups show steadier values. A chart like this turns a vague question into a visible answer within a minute.

Common Beginner Mistakes

  • Forgetting plt.show() in scripts, which leaves the chart undisplayed.
  • Plotting text columns on a numeric axis.
  • Using too many colors in one chart.
  • Leaving out titles and axis labels.

Key Points

  • Seaborn creates attractive statistical charts from Pandas tables.
  • Match the chart type to the question you want to answer.
  • Pair plots and heatmaps give fast overviews of new datasets.