catplot in sns showing intersections which are actually not there

Viewed 146

I am trying to create a catplot in sns and the sample data is below https://drive.google.com/file/d/1Lq3KH6Mj1WVPNA4ByJwtFQd4ovLd4lgi/view?usp=sharing

when I use the below to generate the plot

fig=sns.catplot(data=df, x = 'SubCategory', hue='filedate',col='Category',kind='count')

I get the below plot.

enter image description here

Why does the SubCategory like "G" come in Category 1 and 2 when it is only in 3. Also how can I just bring those SubCategories which are in respective Category instead of all SubCategories showing up against all the Categories

1 Answers

Don't think it's possible to drop the categories with count = 0, you can try to define a custom function and call FacetGrid , which catplot uses:

import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

df = pd.read_csv("./Downloads/zrr2weeks.csv")

def bplot(x,y,**kwargs):
    sns.countplot(x,hue=y,**kwargs)
g = sns.FacetGrid(df,col='Category',sharex=False)
g.map(bplot,'SubCategory','filedate')

enter image description here

You can see in Category 8 there are two Gs which are "G" and "G ". Not very sure if this is intended. If not:

df.SubCategory = df.SubCategory.str.replace(" ","")
g = sns.FacetGrid(df,col='Category',sharex=False)
g.map(bplot,'SubCategory','filedate')

enter image description here

Related