Home > Software engineering >  How do I plot countplot using the same column from multiple similar datasets?
How do I plot countplot using the same column from multiple similar datasets?

Time:01-17

I am trying to plot a countplot with seaborn using multiple datasets which were sliced from a single dataset( sns.countplot using new column

A sns.catplot() using the new column as col= could look like:

sns.set()
g = sns.catplot(data=df, x='Sex', col='Category', sharey=True, height=4, aspect=0.5, palette='rocket', kind='count')
g.set(xlabel='')
plt.tight_layout()
plt.show()

sns.catplot using new column as col=

CodePudding user response:

You should put your resulting dataframes in a list and use a for loop to iterate over them one by one. Putting the relevant code in a function also helps you to not repeat yourself.

def plot_counts(dataframe):
    dataframe['Sex'].value_counts()
    sns.countplot(data=dataframe, x='Sex')

df = pd.read_csv("heart.csv")
plot_counts(df)

Clv = df.loc[0:302, :]
Hng = df.loc[303:(303 293), :]
Swtz = df.loc[(303 294):(303 294 122), :]
Lb = df.loc[(303 294 123):(303 294 123 199), :]
Stl = df.loc[(303 294 123 200):, :]
df_list = [Clv, Hng, Swtz, Lb, Stl]

for dataframe in df_list:
    plot_counts(dataframe)

If you only need to make the 'Sex' column once, you can eliminate the function definition and just call sns.countplot() directly:

df = pd.read_csv("heart.csv")
df['Sex'].value_counts()
sns.countplot(data=df, x='Sex')

Clv = df.loc[0:302, :]
Hng = df.loc[303:(303 293), :]
Swtz = df.loc[(303 294):(303 294 122), :]
Lb = df.loc[(303 294 123):(303 294 123 199), :]
Stl = df.loc[(303 294 123 200):, :]
df_list = [Clv, Hng, Swtz, Lb, Stl]

for dataframe in df_list:
    sns.countplot(data=dataframe, x='Sex')
  •  Tags:  
  • Related