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.
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:
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:
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:
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:
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:
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:
- '
barfor aggregated means and confidence intervals' - '
boxfor distributions and quartiles' - '
violinfor distribution shape' - '
striporswarmfor 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
- '
catplotworks 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
kindbased 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.meltis the usual way to do that transformation.' - Use
huewhen you want multiple former columns in one plot, orcolwhen you want separate facets. - The key idea is tidy data, not a special multi-column plotting flag.
Related reading
- Plot two histograms on single chart
- Plot yerr/xerr as shaded region rather than error bars
- Plotly How to make an annotated confusion matrix using a heatmap?
- Plotting a 2D heatmap
- Plotting a pie chart out of a dictionary
- Plotting a ROC curve in scikit yields only 3 points
- Plotting a list of x, y coordinates
- Plotting decision boundary for High Dimension Data
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.