Search code examples
seaborn

Plot the results of counting values with Seaborn


I have a dataframe of medical data with a Class column that represents the outcome of a type of cancer. The value 2 is for benign and the value 4 is for malign. So far my code is the following:

b,m=df["Class"].value_counts()

so when I print both values I got:

b= 450 m= 250

and I want to display those values on a barplot using seaborn, so I made the following:

sns.countplot(data=df["Class"].value_counts(),x=["benign","malign"])

The problem is that it prints only one value like this:

enter image description here

What am I missing? Also the scale of count only displays from 0.0 to 1.0 and I would like to display the real values.


Solution

  • With sns.countplot, you don't need to manually calculate the various types of counts inside df['class']. Since there is no dataset you use, here's a demo:

    import seaborn as sns
    
    titanic = sns.load_dataset("titanic")
    ax = sns.countplot(x="class", data=titanic)
    

    demo

    In summary, you only need to provide data and x for sns.countplot to achieve your desired effect. In your example, it may be like this:

    sns.countplot(x="Class", data=df)
    

    good luck!