Home > Back-end >  How do I create a count plot with multiple columns without the axes being stored in a numpy.ndarray?
How do I create a count plot with multiple columns without the axes being stored in a numpy.ndarray?

Time:06-14

I'm new to coding and this is my first post. Sorry if it could be worded better!

I'm taking a free online course, and for one of the projects I have to make a count plot with 2 subplot columns.

I've managed to make a count plot with multiple subplots using the code below, and all of the values are correct.

fig = sns.catplot(x = 'variable', hue = 'value', order = ['active', 'alco', 'cholesterol', 'gluc', 'overweight', 'smoke'], col='cardio', data = df_cat, kind = 'count')

But because of the way I've done it, the fig.axes is stored in a 2 dimensional array. The only difference between both rows of the array is the title (cardio = 0 or cardio = 1). I'm assuming this is because of the col='cardio'. Does the col argument always cause the fig.axes to be stored in a 2D array? Is there a way around this or do I have to completely change how I'm making my graph?

I'm sure it's not usually a problem, but because of this, when I run my program through the test module, it fails since some of the functions in the test module don't work on numpy.ndarrays.

I pass the test if I change the reference from fig.axes[0] to fig.axes[0,0], but obviously I cant just change the test module to pass.

CodePudding user response:

I found something. This is just an implementation detail, so it would be nuts to rely on it. If you set col_wrap, then you get an axes ndarray of a different shape.

Reproduced like this:

import seaborn as sns 

# I don't have your data but I have this example
tips = sns.load_dataset("tips")

fig = sns.catplot(x='day', hue='sex', col='time', data=tips, kind='count', col_wrap=2)
fig.axes.shape

And it has shape (2,) i.e it's 1D. seaborn==0.11.2.

  • Related