[英]How to plot multiple columns side by side with Seaborn countplot
我有一個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]
})
我想要每個option A, B and C
的seaborn countplot
圖,並排繪制每列的top 2
。
下面是類似的東西的圖像。 我的問題是將列與圖例並排放置。
謝謝
你能試試這個嗎?
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=[])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.