如何与Seaborn countplot并排绘制多列

时间:2020-03-11 13:31:23

标签: python pandas matplotlib data-visualization seaborn

我有一个null,例如:

Pandas DataFrame

我希望每个pd.DataFrame({ "A": [165,166,920,920,920,165,162,162,166,165,755,469], "B": [1920,868,470,470,698,945,1670,575,701,1920,1920,470], "C": [1670,461,461,212,212,212,212,414,453,196,254,461] }) 中有一个seaborn countplotoption A, B and C并排绘制的每一列。

下面是类似图片。我的问题是使列与图例并列。

Something similar to this

谢谢

1 个答案:

答案 0 :(得分:1)

你可以试试吗?

import pandas as pd
import seaborn as sns

df=pd.DataFrame({
"A": [165,166,920,920,920,165,162,162,166,165,755,469],
"B": [1920,868,470,470,698,945,1670,575,701,1920,1920,470],
"C": [1670,461,461,212,212,212,212,414,453,196,254,461]})


sns.set(style="darkgrid")
fig, ax =plt.subplots(3,1)
max_count = max([max(df[i].value_counts()) for i in df.columns])
A=sns.countplot(y=df['A'],ax=ax[0],order=df.A.value_counts().iloc[:2].index)
B=sns.countplot(y=df['B'],ax=ax[1],order=df.B.value_counts().iloc[:2].index)
C=sns.countplot(y=df['C'],ax=ax[2],order=df.C.value_counts().iloc[:2].index)
ax[0].set_xlim(0,max_count)
ax[1].set_xlim(0,max_count)
ax[2].set_xlim(0,max_count)
A.set(xticklabels=[])
B.set(xticklabels=[])

enter image description here