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

Python Matplotlib/Seaborn Countplot在一个图中具有不同类别,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我有两个变量长度和数量不同的序列,我想画出每个序列中每个变量(名称)出现的频率。 我希望系列1有一个灰色的计数图,系列2有一个红色的计数图,我希望它们相互重叠显示。 然而,由于第2辑缺少“Nancy”,它也减少了第1辑中“Nancy”的数量。 我怎样才能得到一个完整的覆盖两个系列包括一个酒吧南希 import matplotlib.pyplot as plt import seaborn as sns ser1 = pd.Series( ['tom','tom','bob','bob','na

我有两个变量长度和数量不同的序列,我想画出每个序列中每个变量(名称)出现的频率。 我希望系列1有一个灰色的计数图,系列2有一个红色的计数图,我希望它们相互重叠显示。 然而,由于第2辑缺少“Nancy”,它也减少了第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()

编辑: 更改为以下选项将再次导致问题。如何使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()

可以存储第一个打印的设置,并在打印第二个打印后恢复设置

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','bob'])

fig = plt.figure()
ax = sns.countplot(x=ser1, color='grey')
ticks = ax.get_xticks()
ticklabels = ax.get_xticklabels()
lim = ax.get_xlim()

sns.countplot(x=ser2, color='red')
ax.set_xlim(lim)
ax.set_xticks(ticks)
ax.set_xticklabels(ticklabels)
plt.show()

另一个选项可以是先绘制第二个绘图,但将zorder设置为更高的值,以便这些条显示在后面的绘图之前

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','bob'])

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

plt.show()
在更一般的情况下,您需要使用
order
arment

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

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

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

plt.show()

如果您希望使用matplotlib的分类来创建绘图,则如下所示:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

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

u1, counts1 = np.unique(ser1.values, return_counts=True)
u2, counts2 = np.unique(ser2.values, return_counts=True)

fig, ax = plt.subplots()
ax.bar(u1,counts1, color='grey')
ax.bar(u2,counts2, color='red')

plt.show()

感谢您的快速解决方案!我现在正在使用zorder技巧。不幸的是,matplotlib无法识别类别。如果我将序列更改为:ser1=pd.series(['tom','tom','bob','bob','nancy','zulu'])ser2=pd.series(['tom','nancy']),结果将不正确。即使我用zulu更新了系列数据类型(“类别”)的答案,它也能按预期工作。如果您将ser2更改为ser2=pd.series(['tom','nancy']),类别将不再匹配。我更新了答案。请注意,这里使用的是seaborn countplot,因此它不使用matplotlib分类轴,而是seaborn在内部创建的数字轴。我还添加了一个带有matplotlib的解决方案,用于比较。