[英]Python: Plotting percentage in seaborn bar plot
對於數據框
import pandas as pd
df=pd.DataFrame({'group':list("AADABCBCCCD"),'Values':[1,0,1,0,1,0,0,1,0,1,0]})
我正在嘗試繪制一個條形圖,顯示A, B, C, D
取零(或一)的時間百分比。
我有一個可行的方法,但我認為必須有更直接的方法
tempdf=df.groupby(['group','Values']).Values.count().unstack().fillna(0)
tempdf['total']=df['group'].value_counts()
tempdf['percent']=tempdf[0]/tempdf['total']*100
tempdf.reset_index(inplace=True)
print tempdf
sns.barplot(x='group',y='percent',data=tempdf)
如果它只是繪制平均值,我可以簡單地在df
數據幀上做sns.barplot
而不是 tempdf。 如果我對繪制百分比感興趣,我不確定如何優雅地做到這一點。
謝謝,
您可以將 Pandas 與 seaborn 結合使用以簡化此操作:
import pandas as pd
import seaborn as sns
df = sns.load_dataset("tips")
x, y, hue = "day", "proportion", "sex"
hue_order = ["Male", "Female"]
(df[x]
.groupby(df[hue])
.value_counts(normalize=True)
.rename(y)
.reset_index()
.pipe((sns.barplot, "data"), x=x, y=y, hue=hue))
您可以在sns.barplot
estimator
使用自己的函數,如文檔所示:
estimator : 可調用的映射向量 -> 標量,可選
在每個分類箱內估計的統計函數。
對於您的情況,您可以將函數定義為 lambda:
sns.barplot(x='group', y='Values', data=df, estimator=lambda x: sum(x==0)*100.0/len(x))
您可以按照以下步驟操作,以便您可以看到圖中條形頂部的計數和百分比。 檢查下面的示例輸出
如果您的圖中有“色調”參數, with_hue函數將在條形圖上繪制百分比。 它以實際圖形、特征、特征中的 Number_of_categories 和hue_categories(色調特征中的類別數)作為參數。
如果您有正常繪圖, without_hue函數將在條形圖上繪制百分比。 它以實際圖形和特征為參數。
def with_hue(plot, feature, Number_of_categories, hue_categories):
a = [p.get_height() for p in plot.patches]
patch = [p for p in plot.patches]
for i in range(Number_of_categories):
total = feature.value_counts().values[i]
for j in range(hue_categories):
percentage = '{:.1f}%'.format(100 * a[(j*Number_of_categories + i)]/total)
x = patch[(j*Number_of_categories + i)].get_x() + patch[(j*Number_of_categories + i)].get_width() / 2 - 0.15
y = patch[(j*Number_of_categories + i)].get_y() + patch[(j*Number_of_categories + i)].get_height()
ax.annotate(percentage, (x, y), size = 12)
plt.show()
def without_hue(plot, feature):
total = len(feature)
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_height()/total)
x = p.get_x() + p.get_width() / 2 - 0.05
y = p.get_y() + p.get_height()
ax.annotate(percentage, (x, y), size = 12)
plt.show()
您可以使用庫 Dexplot ,它能夠返回分類變量的相對頻率。 它具有與 Seaborn 類似的 API。 將您想要獲取相對頻率的列傳遞給count
函數。 如果您想將其細分為另一列,請使用split
參數執行此操作。 以下返回原始計數。
import dexplot as dxp
dxp.count('group', data=df, split='Values')
要獲得相對頻率,請將normalize
參數設置為要normalize
的列。 使用True
對總計數進行標准化。
dxp.count('group', data=df, split='Values', normalize='group')
對'Values'
列進行標准化將生成下圖,其中所有“0”條的總和為 1。
dxp.count('group', data=df, split='Values', normalize='Values')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.