Shown below is the syntax used to get the bar char for a categorical data on seaborn
JavaScript
x
20
20
1
import seaborn as sn
2
import matplotlib as mpl
3
import matplotlib.pyplot as plt
4
5
6
IN: data['coast'].dtypes
7
OUT:
8
CategoricalDtype(categories=[0, 1], ordered=False)
9
10
11
IN: data['coast'].value_counts()
12
OUT:
13
0 21450
14
1 163
15
Name: coast, dtype: int64
16
17
18
IN: sn.factorplot('coast', data=data, kind='count')
19
OUT:
20
Advertisement
Answer
Maybe this will work for you:
JavaScript
1
26
26
1
# imports
2
import sys # for retreiving package version matplotlib
3
import numpy as np
4
import pandas as pd
5
import matplotlib.pyplot as plt
6
import seaborn as sns
7
8
# package versions
9
print('numpy :', np.__version__)
10
print('pandas :', pd.__version__)
11
print('matplotlib :', sys.modules['matplotlib'].__version__)
12
print('seaborn :', np.__version__)
13
14
# set seed for reproducibility
15
np.random.seed(100)
16
17
# generate data
18
n = 15
19
data = pd.DataFrame({'coast': np.random.randint(low=0, high=2, size=n, dtype=int)})
20
data['coast'] = data['coast'].astype('category')
21
22
# plot data
23
ax = sns.countplot(x='coast', data=data)
24
plt.bar_label(ax.containers[0]) # plot bar labels
25
plt.show()
26
Results:
JavaScript
1
5
1
numpy : 1.21.0
2
pandas : 1.3.0
3
matplotlib : 3.4.2
4
seaborn : 1.21.0
5