Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Data and Figure Basics

Datasets and load_dataset

Use seaborn's example datasets (tips, penguins, iris, flights), read your own CSV files with pandas, set categorical order and handle missing values.

Seaborn ships a helper, sns.load_dataset, that fetches small example tables such as tips, penguins, iris, flights, titanic, diamonds and mpg. They are what most seaborn documentation and blog posts use, so you will meet them constantly. Real work, though, starts with your own data. This lesson covers both, plus the cleanup steps (types, order, missing values) that make plots behave.

The example datasets

Python
import seaborn as sns

print(sns.get_dataset_names()[:6])     # lists the available names
tips = sns.load_dataset("tips")
penguins = sns.load_dataset("penguins")

load_dataset downloads a CSV file from seaborn's data repository on GitHub, then caches it in a folder called seaborn-data in your home directory. It needs an internet connection the first time. Some datasets and what they are good for:

Dataset Rows Typical use
tips 244 Scatter, bar and box plots: bill, tip, day, smoker
penguins 344 Distributions and pair plots: three species, four measurements
iris 150 Classic species comparison
flights 144 Heatmaps and line plots over time
titanic 891 Categorical plots with survival
mpg 398 Car fuel efficiency and regression
diamonds 53,940 Large-data distributions
planets 1,035 Skewed distributions, log scales

Browsers run our examples in a sandbox where a download from GitHub may be blocked, so the runnable examples in this course build their own tables with a fixed random seed. The plotting code is identical: swap in sns.load_dataset("tips") on your own machine.

Your own data with pandas

Anything pandas can read, seaborn can plot: CSV, Excel, JSON, SQL queries, Parquet. The usual first steps:

Python
import pandas as pd

df = pd.read_csv("sales.csv", parse_dates=["order_date"])
df.info()          # column names, types, missing counts
df.head()
df.describe()

Check info() before plotting. A column of numbers read as text (object) will be treated as categorical by seaborn, producing a scatter plot with thousands of tick labels. Convert it with pd.to_numeric(df["col"], errors="coerce").

Python Read a CSV and fix the types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

io.StringIO lets us pretend a string is a file. With a real file you would write pd.read_csv("sales.csv"). Notice that the empty rating became NaN: seaborn skips missing values in most plots rather than failing, but you should know about them (df.isna().sum()).

Categorical types and order

Seaborn plots categories in the order they appear in the data, or alphabetically if the column is numeric-like. For days or sizes, that is usually wrong. You have two fixes.

Per call, with order= (and hue_order=):

Python
sns.boxplot(data=tips, x="day", y="total_bill", order=["Thur", "Fri", "Sat", "Sun"])

Once, in the data, with an ordered categorical type, so every later plot is right:

Python
tips["day"] = pd.Categorical(tips["day"], categories=["Thur", "Fri", "Sat", "Sun"], ordered=True)

The example datasets already do this for some columns, which is why tips plots Thur to Sun in order.

Python Ordered categories fix the axis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Missing values

Most seaborn functions drop rows with NaN in the plotted columns. That is convenient but can hide problems: a half-empty column quietly makes a smaller sample. Decide on purpose:

Python
df = df.dropna(subset=["rating"])        # drop rows where rating is missing
df["rating"] = df["rating"].fillna(df["rating"].median())   # or fill

Dropping is fine for exploration. For results you report, state how many rows were removed.

A tiny data-prep checklist

  1. df.info(): are numeric columns numeric, and dates real datetimes?
  2. df.isna().sum(): how much is missing, and where?
  3. Strings: strip spaces, fix inconsistent capitalization ("north" vs "North").
  4. Categories: set the order of days, months, sizes, ratings.
  5. Units: convert so one column means one thing.
  6. Row count: print len(df) before and after filtering.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does sns.load_dataset('tips') need the first time it runs?

    The example data lives in a GitHub repository. load_dataset downloads it once and caches it locally.
  2. 2. A numeric column was read as text. How does seaborn behave when you use it for x in scatterplot?

    Text values are categorical. Convert with pd.to_numeric before plotting.
  3. 3. Which is the best way to make every plot show days as Mon, Tue, Wed?

    An ordered categorical keeps the order in the data; order= is the per-call version.
  4. 4. What do most seaborn functions do with rows that contain NaN in the plotted columns?

    Missing values are dropped for the plot. That is convenient, but check how many rows you lose.

Summary

  • sns.load_dataset downloads small example tables (tips, penguins, iris, flights, ...); it needs internet the first time.
  • Real data comes in through pandas (read_csv, read_excel, read_sql).
  • Check types with info(); text numbers and strings for dates cause odd plots.
  • Control category order with order= or an ordered pd.Categorical.
  • Missing values are dropped by most plots; count and report them.

Next: Mapping Data to Color, Size and Style