小编Rol*_*olo的帖子

Matplotlib / Seaborn Countplot 在一个图中具有不同的类别

我有两个具有不同长度和变量数量的系列,并且想要绘制每个系列中每个变量(名称)出现的频率。我想要系列 1 的灰色计数图和系列 2 的红色计数图,我希望它们彼此重叠显示。然而,由于系列 2 缺少“南希”,它也减少了系列 1 的“南希”计数。我如何获得包含 Nancy 酒吧的两个系列的完整叠加?

import matplotlib.pyplot as plt
import seaborn as sns

ser1 = pd.Series( ['tom','tom','bob','bob','nancy'])
ser2 = pd.Series( ['tom','bob'])

fig = plt.figure()
sns.countplot(x=ser1, color='grey')
sns.countplot(x=ser2, color='red')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

编辑:更改为以下将再次导致问题。我如何让 Matplotlib 识别出这两个系列具有相同的正在计算的分类值?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

ser1 = pd.Series( ['tom','tom','bob','bob','nancy','zulu'])
ser2 = pd.Series( ['tom','nancy'])

ser1 = ser1.astype('category')
ser2 = ser2.astype('category')

fig = plt.figure()
ax = sns.countplot(x=ser2, color='red', zorder=2)
sns.countplot(x=ser1, color='grey')

plt.show()
Run Code Online (Sandbox Code Playgroud)

python matplotlib seaborn

2
推荐指数
1
解决办法
2456
查看次数

标签 统计

matplotlib ×1

python ×1

seaborn ×1