简体   繁体   English

用每个类别的行数注释 seaborn 线图图例

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

I want to get and show the number of rows of each type in a seaborn lineplot.我想在 seaborn 线图中获取并显示每种类型的行数。 Eg例如

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

I want to show the number of rows with event 'stim' and number of rows with event 'cue' as an addition to the legend eg instead of showing 'stim' in the legend, it could show 'stim (23)' meaning that 23 rows have the event as 'stim'我想显示事件“stim”的行数和事件“cue”的行数作为图例的补充,例如,而不是在图例中显示“stim”,它可以显示“stim (23)”,这意味着23 行的事件为 'stim'

Something like this would do the trick:像这样的东西可以解决问题:

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