Reputation: 3528
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:
Upvotes: 7
Views: 6940
Reputation: 339122
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()
Upvotes: 13