如何在seaborn中绘制选定的Y值



我试图在Seaborn中创建一个随时间变化的癌症发病率图。我的问题是,我只想为这个报告绘制选定的y轴值。例如,在按县计算每年的癌症发病率时,我希望能够只绘制几个选定县的图表,而不是所有县。下面是我使用的代码,它按预期返回所有县的结果(y)。我如何修改它,只显示某些(例如:洛杉矶,约洛,阿拉米达等)?

sns.relplot(x='Year',y = 'cancer/100k pop' , data = dataset_all,hue="County", kind="line",ci=None)
title_string = "Trend of " 
plt.xlabel('Years')
plt.title(title_string)

谢谢!

假设dataset_all是一个pandas数据框,最简单的解决方案是过滤数据。

例如,

display_counties = ['Los Angeles', 'Yolo', 'Alameda']
subset = dataset_all.loc[dataset_all['County'].isin(display_counties), :]
sns.relplot(x='Year',y='cancer/100k pop', data=subset, hue="County", kind="line",ci=None)
title_string = "Trend of " 
plt.xlabel('Years')
plt.title(title_string)

最新更新