0

I have categorical variables in my data set, most of them are binary 0,1 but some are multi-class. I used countplot to plot the distribution.

f, axes = plt.subplots(4,3,figsize=(17,13), sharex=True)
for i, feature in enumerate(cat_var_list):
    sns.countplot(df[feature],ax=axes[i%4, i//4])

cat_var_list has 12 variables.

However I found that the scale is 0,1 and variables that have multi-class outcomes 0,1,2 do not show properly.

For example, the plot looks like this:

enter image description here

However, for the variable Intro Election Status, the plot should look like this:

enter image description here

How can I make the plot show up properly in the multi plot grid format?

1 Answer 1

2

I see your code works as expected with this sample data:

np.random.seed(1)
df = pd.DataFrame(np.random.choice([0,1], (100,11)),
                  columns=list('abcdefABCDE'))
df['F'] = np.random.choice([0,1,2], 100)

cat_var_list = 'abcdefABCDEF'

f, axes = plt.subplots(4,3,figsize=(17,13), sharex=True)
for f,ax in zip(cat_var_list, axes.ravel()):
    sns.countplot(df[f], ax=ax)
    ax.set_title(f)

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I removed sharex=True and it worked, I am still not sure why though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.