7

I have the following data:

male      843
female    466
Name: Sex, dtype: int64

I plotted bar plots for the same using countplot from seaborn, and it worked.

But I would like to know what could be its alternative in matplotlib.

I did:

sns.countplot(x = 'Sex', data = complete_data)

It gave me:

enter image description here

1 Answer 1

13

Say you have this data:

import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Sex" : np.random.choice(["male",  "female"], size=1310, p=[.65, .35]),
                   "other" : np.random.randint(0,80, size=1310)})

You can plot a countplot in seaborn as

import seaborn as sns
sns.countplot(x="Sex", data=df)
plt.show()

Or you can create a bar plot in pandas

df["Sex"].value_counts().plot.bar()
plt.show()

Or you can create a bar plot in matplotlib

counts = df["Sex"].value_counts()
plt.bar(counts.index, counts.values)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

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.