Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Data and Figure Basics

Mapping Data to Color, Size and Style

Learn how seaborn maps columns to color (hue), size and marker style, how categorical and numeric variables differ, and how to control palettes, order and legends.

The idea that makes seaborn powerful is semantic mapping: instead of picking colors yourself, you tell seaborn which column should control a visual property, and it picks the values. hue maps a column to color, size to marker or line size, and style to marker shape or dash pattern. Understanding how seaborn builds these mappings explains most of its behavior, including its legends.

The three semantics

Three visual channels

hue Color of points, lines or bars.
size Area of markers or width of lines.
style Marker shape or dash pattern.

Each takes a column name, and seaborn builds the legend.

1 / 2

Each function supports a different subset. Relational plots (scatterplot, lineplot) accept all three. Distribution and categorical plots mostly use hue. The pattern is always the same: hue="column".

Categorical vs numeric variables

Seaborn looks at the column's data type and picks a different mapping.

  • Categorical or string column to hue: a qualitative palette of distinct colors, one per level (default colors from the deep palette).
  • Numeric column to hue: a sequential colormap, so values map to a gradient and the legend shows a few sample values.
  • Numeric column to size: sizes scale continuously between a minimum and maximum (sizes=(20, 200)).
  • Categorical column to size: discrete sizes in order of the levels, usually only sensible for ordered categories.

A numeric column that holds category codes (1, 2, 3 meaning groups) is treated as a number. Convert it with df["cluster"].astype(str) or astype("category") to get distinct colors.

Python Categorical vs numeric hue

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.

Controlling the mapping

Each semantic has companion arguments:

Semantic Arguments What they do
hue palette, hue_order, hue_norm Colors, order of levels, the value range mapped to a numeric gradient
size sizes, size_order, size_norm Minimum and maximum sizes, order, range
style markers, dashes, style_order Shapes (for example ["o", "s", "^"]), line dashes, order

Examples:

Python
sns.scatterplot(data=df, x="x", y="y", hue="group",
                hue_order=["low", "medium", "high"],
                palette={"low": "#4c72b0", "medium": "#dd8452", "high": "#c44e52"})

sns.scatterplot(data=df, x="x", y="y", size="weight", sizes=(20, 300), alpha=0.6)
sns.scatterplot(data=df, x="x", y="y", style="group", markers=["o", "s", "^"])

A palette can be a named palette ("Set2", "viridis"), a list of colors, or a dictionary from level to color. The dictionary form guarantees that a level always gets the same color across several figures, which makes a report consistent. The color palettes lesson covers choosing them.

Combining semantics

You can map different columns to different channels, or the same column to two:

Python hue plus style for accessibility

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.

Mapping group to both hue and style (left) is a good habit: readers with color-vision differences, or a black-and-white printout, can still tell the groups apart. The right plot encodes four variables (x, y, group, age), which is information-dense; do it only when each channel earns its place.

Legends

Seaborn adds a legend whenever a semantic is used. Control it with legend="auto" (default), "brief" (fewer entries for numeric variables), "full" (every level) or False. Move it with sns.move_legend as covered in Labels, Titles and Legends.

Common mistakes

  • Mapping a continuous variable to style: seaborn has only a handful of markers and raises an error for many levels.
  • Too many hue levels: beyond about eight colors it is hard to tell them apart. Facet with col= instead.
  • Passing a numeric code column to hue and wondering why it shows a gradient.
  • Setting color= and hue= together: hue wins; use color only without hue.
  • Using palette= without hue=: it is ignored in some functions and warns in newer versions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You pass a column of integers 1, 2, 3 (cluster labels) to hue. How does seaborn color the points?

    Numeric data gets a sequential colormap. Convert the column to strings or categories to get distinct colors.
  2. 2. Which argument sets the range of marker areas when you map a numeric column to size?

    sizes is the (min, max) of the size scale.
  3. 3. Why map the same column to both hue and style?

    Redundant encoding makes a chart accessible.
  4. 4. How do you guarantee that 'low' is always the same color in every figure of a report?

    A dictionary palette pins each level to a color regardless of which levels appear in a particular plot.

Summary

  • hue, size and style map data columns to color, size and marker/dash style.
  • Categorical columns get distinct values; numeric columns get gradients and continuous sizes.
  • Tune with palette, hue_order, sizes, markers, dashes and *_norm.
  • Dictionary palettes keep colors consistent across figures.
  • Pair hue with style for accessibility; avoid too many levels.

Next: Saving Seaborn Figures