在 Seaborn 的基于 FacetGrid 的线图中,想更改标签标题。这似乎是一件简单的事情,但结果却是一项艰巨的任务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()传说标题“性”我想通过添加“标题”参数将标签标题从“性别”更改为“性别”。但事实证明,成为现有标题之上的标题g.add_legend(title = 'Gender')图例标题为“性别”,标题为“性别”我还尝试访问 fig.legend 来更改文本,但现在它显示了多个图例,可能是由于多方面的情节。l = g.fig.legend()l.texts[0].set_text('Gender')图例标题“性别”,然而,有多个图例我确信可能有一种“hacky”方式通过更改数据中的变量名称来更改名称,但我想知道是否有一种方法可以简单地替换 Seabron FacetGrid 的图例标题,或者,如果不可能,通过'fig.legend'添加标题,同时显示单个相关图例。非常感谢!
1 回答

千万里不及你
TA贡献1784条经验 获得超9个赞
为什么不将"sex"列的名称替换为"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()
添加回答
举报
0/150
提交
取消