question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use catplot with melt for different categories

See original GitHub issue

I would have expected that I can visualize different categorical variables in a count plot using catplot and melt. However, it looks like the data is converted to categorical before grouping for the columns, and so the categories are shared among all the count plots. That doesn’t really make sense if the different columns correspond to different categories. Am I overlooking something or is there a different way of doing this?

import pandas as pd
import seaborn as sns
ames = pd.read_excel("http://www.amstat.org/publications/jse/v19n3/decock/AmesHousing.xls")

cat_cols = ['MS Zoning', 'Street', 'Alley', 'Lot Shape', 'Land Contour',
       'Utilities', 'Lot Config', 'Land Slope', 'Neighborhood', 'Condition 1',
       'Condition 2', 'Bldg Type', 'House Style', 'Roof Style', 'Roof Matl',
       'Exterior 1st', 'Exterior 2nd', 'Mas Vnr Type', 'Exter Qual',
       'Exter Cond', 'Foundation', 'Bsmt Qual', 'Bsmt Cond', 'Bsmt Exposure',
       'BsmtFin Type 1', 'BsmtFin Type 2', 'Heating', 'Heating QC',
       'Central Air', 'Electrical', 'Kitchen Qual', 'Functional',
       'Fireplace Qu', 'Garage Type', 'Garage Finish', 'Garage Qual',
       'Garage Cond', 'Paved Drive', 'Pool QC', 'Fence', 'Misc Feature',
       'Sale Type', 'Sale Condition']
ames_cat = ames[cat_cols]
sns.catplot(x='value', col='variable', data=ames_cat.melt(), sharex=False, sharey=False, col_wrap=5, kind='count')

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
mwaskomcommented, Mar 21, 2019

Indeed, catplot has some logic that unifies categories across the facets, because sharex (which just gets passed down to subplots) won’t do it on its own. But it doesn’t not do that if the user specifies sharex=False.

(Notes to myself: this is a bit tricky because it will have to figure out from the _CategoricalPlotter which way the plot is oriented, i.e. it’s not as simple as depending on sharex).

Anyway you can use FacetGrid directly, which usually is risky because the categorical axis won’t be shared, but that’s what you want here:

g = sns.FacetGrid(ames_cat.melt(), col="variable", sharex=False, sharey=False, col_wrap=5)
g.map(sns.countplot, "value", order=None)

(If I understand what you’re aiming for, this should do it).

1reaction
amuellercommented, Mar 22, 2019

It does! someone should collect all your issue replies and make a seaborn cook-book. Thank you for your quick and insightful reply!.

I’m not sure if I understand your explanation as I’m using sharex=False and I still observe the behavior. But I have the solution I was looking for!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a restriction on catplot with subplot? - Stack Overflow
Catplot is a figure-level function whereas you cannot use axes. Try using stripplot instead. fig, axs = plt.subplots (1, 2, figsize=(25, ...
Read more >
seaborn.catplot — seaborn 0.12.1 documentation - PyData |
This function provides access to several axes-level functions that show the relationship between a numerical and one or more categorical variables using one ......
Read more >
Three common seaborn difficulties | by Michael Waskom
This post aims to explain three of the most common difficulties encountered by users of seaborn, a Python library for data visualization.
Read more >
Mastering catplot() in Seaborn with categorical plots
Fortunately, a data visualization library Seaborn encompasses several types of categorical plots into a single function: catplot() .
Read more >
New Plot Types in Seaborn's Latest Release
Seaborn release 0.9 contains several new plot types as well as other ... In practice, Seaborn works best when using Pandas dataframes and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found