Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Matplotlib's alternative for countplot from seaborn?

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

like image 473
Junkrat Avatar asked Jul 20 '26 14:07

Junkrat


1 Answers

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()
like image 163
ImportanceOfBeingErnest Avatar answered Jul 23 '26 05:07

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!