Home > Mobile >  Ploting with seaborn histplot
Ploting with seaborn histplot

Time:02-16

I'm on Jupyter trying to plot a slice of a DataFrame with the following values: Female = 16092 and Male = 27895

sexo = df2.groupby(['Sexo']).size()
sexo

Sexo
F    16092
M    27895
dtype: int64


fig, axs = plt.subplots(1, figsize=[8,8])
sns.histplot(data=sexo, x='Sexo', y=sexo)
plt.show()

But the graph doesn't plot bars with a size relative to the values, just two squares covering a large number of values. (I can't post pictures here in Stack Overflow yet).

I'm probably feeding the histplot with wrong data or should be using another graph.

Thanks in advance.

CodePudding user response:

Use barplot for discrete variables. I created my own dataframe to sort of replicate your situation -> just rewrite below code with your own dataframe

import seaborn as sns
import matplotlib.pyplot as plt
sexo1 = pd.DataFrame({"Gender":['M','F'], "Some_Number":[27895,16092]})
#you can stick to size() 
#but use reset_index() so you can use "Gender" or "Sex" in your graph
sexo2 = sexo1.groupby('Gender').sum().reset_index() 
sexo2

fig, axs = plt.subplots(1, figsize=[8,8])
sns.barplot(data=sexo2, x='Gender', y='Some_Number')
plt.show()

this will output:

enter image description here

CodePudding user response:

try using value_count for discrete categories

df['target'].value_counts()
  • Related