簡體   English   中英

用每個類別的行數注釋 seaborn 線圖圖例

[英]Annotating seaborn lineplot legend with number of rows for each category

我想在 seaborn 線圖中獲取並顯示每種類型的行數。 例如

import seaborn as sns
fmri = sns.load_dataset("fmri")
ax = sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri)

我想顯示事件“stim”的行數和事件“cue”的行數作為圖例的補充,例如,而不是在圖例中顯示“stim”,它可以顯示“stim (23)”,這意味着23 行的事件為 'stim'

像這樣的東西可以解決問題:

fmri = sns.load_dataset("fmri")
x_col = 'timepoint'
y_col = 'signal'
hue_col = 'event'

ax = sns.lineplot(x=x_col, y=y_col, hue=hue_col, data=fmri)
handles,labels = ax.get_legend_handles_labels()
counts = fmri[hue_col].value_counts()
# labels[0] is used for the title by seaborn
new_labels = [labels[0]]+['{:s} ({:d})'.format(l, counts[l]) for l in labels[1:]]
ax.legend(handles, new_labels)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM