seaborn
catplot
data visualization
Python
plotting

Plot seaborn catplots for multiple columns

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Seaborn catplot expects data in tidy or long format, which is why plotting multiple value columns usually starts with reshaping the DataFrame. The standard pattern is to use pandas.melt, then map the old column names into a categorical variable that catplot can facet or color.

Start From Wide Data

Suppose the source data looks like this:

python
1import pandas as pd
2
3df = pd.DataFrame(
4    {
5        "group": ["A", "A", "B", "B"],
6        "score_math": [80, 82, 75, 78],
7        "score_science": [85, 87, 79, 81],
8        "score_english": [77, 79, 73, 74],
9    }
10)
11
12print(df)

This is convenient for storage, but not ideal for catplot because the values you want to compare live in separate columns.

Reshape With melt

Turn the score columns into one subject column and one score column:

python
1long_df = df.melt(
2    id_vars="group",
3    value_vars=["score_math", "score_science", "score_english"],
4    var_name="subject",
5    value_name="score",
6)
7
8print(long_df.head())

After this transformation, each row represents one observation for one subject, which is exactly the structure Seaborn prefers.

Plot With catplot

Now you can plot multiple former columns in one categorical chart:

python
1import seaborn as sns
2import matplotlib.pyplot as plt
3
4sns.catplot(
5    data=long_df,
6    x="group",
7    y="score",
8    hue="subject",
9    kind="bar",
10)
11
12plt.show()

This is the most common answer when someone says "plot catplots for multiple columns." The real trick is not a special multi-column catplot option. It is reshaping the data into tidy form first.

Add Ordering and Labels Explicitly

Once the data is in long form, you can control the display more precisely:

python
1sns.catplot(
2    data=long_df,
3    x="subject",
4    y="score",
5    hue="group",
6    kind="box",
7    order=["score_math", "score_science", "score_english"],
8)
9
10plt.xticks(rotation=20)
11plt.show()

This is helpful when the original column order carries meaning and you do not want Seaborn to rely on default sorting.

Facet Instead of Hue

If separate small multiples are clearer than a color legend, use col:

python
1sns.catplot(
2    data=long_df,
3    x="group",
4    y="score",
5    col="subject",
6    kind="box",
7    sharey=True,
8)
9
10plt.show()

This produces one subplot per former value column. It is often easier to read when there are many categories or when color would become too busy.

Choose the Right Categorical Plot Type

catplot is a figure-level wrapper, so you can switch kind depending on what you want to show:

  • 'bar for aggregated means and confidence intervals'
  • 'box for distributions and quartiles'
  • 'violin for distribution shape'
  • 'strip or swarm for raw observations'

The data reshaping step stays the same. Only the visual summary changes.

Why melt Solves the Multi-Column Problem

The main reason this works is that Seaborn generally wants one column describing "what category is this row" and one column describing "what value should be plotted." Wide data spreads those categories across separate columns, which is why direct plotting feels awkward until you reshape it.

Common Pitfalls

  • 'catplot works best with long-form data, so passing many separate value columns directly usually leads to frustration.'
  • If you melt the data, be careful to keep identifier columns such as group labels in id_vars.
  • Too many hue levels can make the legend hard to read; use faceting when that happens.
  • Choose kind based on whether you want summary statistics or raw distributions.

Summary

  • To plot multiple columns with Seaborn catplot, reshape wide data into long format first.
  • 'pandas.melt is the usual way to do that transformation.'
  • Use hue when you want multiple former columns in one plot, or col when you want separate facets.
  • The key idea is tidy data, not a special multi-column plotting flag.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.