如何更改 Seaborn FacetGrid 的图例标题? [英] How can I change the Seaborn FacetGrid's legend title?

查看:40
本文介绍了如何更改 Seaborn FacetGrid 的图例标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于Seaborn的FacetGrid的线图中,想要更改标签标题.看似简单的事情,结果却是一件毛骨悚然的事情

  tips = sns.load_dataset("tips")g = sns.FacetGrid(提示,col ='day',legend_out = True,)g.map(sns.lineplot,'total_bill','tip','sex','time',ci = 错误)g.fig.legend()

In the Seaborn's FacetGrid based lineplot, would like to change the label title. It seemed like a simple thing, but it turned out to be a hairy task

tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, col= 'day', legend_out= True,)
g.map(sns.lineplot, 'total_bill', 'tip', 'sex', 'time',
     ci = False)    
g.fig.legend()

legend title 'sex'

I wanted to change the label title to 'gender' from 'sex' by adding 'title' argument. But it turns out that become a headline on top the existing title

g.add_legend(title = 'Gender')

legend title 'sex' with headline 'Gender'

I also tried to access the fig.legend to change the text, but now it shows multiple legends, probably due to the multi-faceted plots.

l = g.fig.legend()
l.texts[0].set_text('Gender')

legend title 'Gender', however, with multiple legends

I am sure there may be a 'hacky' way to change the name by changing the variable names in the data or so, but I am wondering whether there is a way to simply replace the Seabron FacetGrid's legend title or, if it is not possible, add the title through 'fig.legend' while showing single relevant legend. Many thanks!

解决方案

Why not replace the name of the "sex" columns with "Gender"?

import matplotlib.pyplot as plt
import seaborn as sns

tips = sns.load_dataset("tips")
tips.columns = [n if n != "sex" else "Gender" for n in tips.columns]

g = sns.FacetGrid(tips, col= 'day')
g.map(sns.lineplot, 'total_bill', 'tip', 'Gender', 'time',
     ci = False)    
g.add_legend()

plt.show()

这篇关于如何更改 Seaborn FacetGrid 的图例标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆