Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Installing Seaborn and Your First Plots

Install seaborn with pip or conda, import it the standard way, set a theme, and draw your first scatter, histogram and bar plots from a DataFrame.

Getting seaborn running takes one command and four imports. This lesson covers installation, the conventional import names, the theme call you will see in almost every script, and three quick plots that show the shape of the API: a scatter plot, a histogram and a bar plot.

Installing

Seaborn needs Python 3.8 or newer, plus NumPy, pandas and Matplotlib. SciPy and statsmodels are optional and only needed for a few features (some statistical functions and regression options).

Terminal
pip install seaborn
# or, with conda:
conda install seaborn
# with the optional extras:
pip install "seaborn[stats]"

Check the installation from Python:

Python
import seaborn as sns
print(sns.__version__)

Install inside a virtual environment, as you would for any project, so versions do not clash. If you use Jupyter, run pip install in the same environment as the notebook kernel; mismatched environments are the usual reason for ModuleNotFoundError.

On this site, each runnable example starts with import micropip and await micropip.install("seaborn"). That installs seaborn inside your browser's Python (it is cached after the first run). Delete those two lines when you run the code on your own machine.

The standard imports

Python
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

The alias sns is a convention from the library's early days and everyone uses it. You will need plt to show figures from scripts, set titles and save files. In Jupyter, figures appear automatically; in a plain script call plt.show().

Setting a theme

sns.set_theme() applies seaborn's default look to every plot that follows: light grid, softer colors, larger fonts. It changes Matplotlib's global settings, so call it once near the top.

Python
sns.set_theme()                      # defaults
sns.set_theme(style="whitegrid")     # a different background
sns.set_theme(context="talk", palette="muted")

Themes get a full lesson in Themes, Styles and Plotting Contexts. The defaults are enough to begin.

Three plots in three lines

A single table will serve for this chapter. It simulates restaurant visits: bill amount, tip, day, and party size. We build it with a fixed seed so the numbers repeat.

Python Scatter, histogram and bar plot

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.

Notice the pattern shared by all three calls:

  1. data= names the DataFrame.
  2. x= and y= name columns, as strings.
  3. The function returns a Matplotlib Axes, and ax= lets you place a plot on an existing axes (here, one panel of three).

The bar plot did real work: for each day it computed the mean tip, and the thin black line is a 95% confidence interval from bootstrapping. You got statistics without asking.

Plots from arrays and Series

A DataFrame is optional. You can pass arrays, lists, or pandas Series directly:

Python
sns.histplot(x=[3, 4, 4, 5, 5, 5, 6, 6, 7])
sns.scatterplot(x=tips["total_bill"], y=tips["tip"])

Seaborn labels the axes using the Series name. Plain lists have no name, so the axis label is blank. Using data= with strings is cleaner and lets you add hue, size and style columns later without changing the call.

Where plots go: scripts, notebooks, files

Environment How you see the figure
Jupyter or Colab The last expression of a cell displays inline
A Python script Call plt.show() to open a window
A server or CI job Use plt.savefig("plot.png"), or plt.close() after saving

Saving figures has details on formats and resolution.

Common beginner problems

  • ModuleNotFoundError: No module named 'seaborn': the package is installed in a different environment from the one running your code.
  • Nothing appears in a script: add plt.show().
  • A ValueError that a variable is not found: the string you passed to x= or y= must match a column name exactly, including case. Print df.columns to check.
  • Old tutorials use sns.set(): it still works, but set_theme() is the current name. Functions such as distplot and factorplot are removed; use displot/histplot and catplot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the conventional import line for seaborn?

    sns is the universal alias, so code you read and write stays consistent.
  2. 2. You run a script that calls sns.histplot(...) but no window appears. What is the most likely fix?

    Outside notebooks, Matplotlib needs plt.show() to open the figure window (or savefig to write a file).
  3. 3. What does the data= argument expect?

    With data=, the x, y and hue arguments are column names looked up in that table.
  4. 4. What does sns.barplot show by default for each bar?

    barplot aggregates with the mean and draws a bootstrapped 95% confidence interval. Use countplot to count rows.

Summary

  • Install with pip install seaborn; import it as sns next to numpy, pandas and matplotlib.pyplot.
  • sns.set_theme() applies the default look globally.
  • Most calls follow sns.plotname(data=df, x="col", y="col", hue="col").
  • Seaborn functions return Matplotlib axes; ax= places a plot on an axes you created.
  • Bar plots and similar functions compute statistics for you.
  • Use plt.show() in scripts and plt.savefig() for files.

Next: Figure-Level vs Axes-Level Functions