简体   繁体   English

Seaborn pairplot:如何更改图例标签文本

[英]Seaborn pairplot: how to change legend label text

I'm making a simple pairplot with Seaborn in Python that shows different levels of a categorical variable by the color of plot elements across variables in a Pandas DataFrame.我正在用 Python 中的 Seaborn 制作一个简单的配对图,它通过 Pandas DataFrame 中变量的绘图元素的颜色来显示分类变量的不同级别。 Although the plot comes out exactly as I want it, the categorical variable is binary, which makes the legend quite meaningless to an audience not familiar with the data (categories are naturally labeled as 0 & 1).尽管情节完全符合我的要求,但分类变量是二元的,这使得图例对于不熟悉数据的观众来说毫无意义(类别自然标记为 0 和 1)。

An example of my code:我的代码示例:

g = sns.pairplot(df, hue='categorical_var', palette='Set3')

Is there a way to change legend label text with pairplot?有没有办法用pairplot更改图例标签文本? Or should I use PairGrid, and if so how would I approach this?或者我应该使用 PairGrid,如果是这样,我将如何处理?

Found it!找到了! It was answered here: Edit seaborn legend答案在这里: 编辑seaborn传说

g = sns.pairplot(df, hue='categorical_var', palette='Set3')
g._legend.set_title(new_title)

Since you don't provide a full example of code, nor mock data, I will use my own codes to answer.由于您没有提供完整的代码示例,也没有提供模拟数据,我将使用我自己的代码来回答。

First solution第一个解决方案

The easiest must be to keep your binary labels for analysis and to create a column with proper names for plotting.最简单的方法是保留二进制标签以供分析,并创建一个具有适当名称的列以进行绘图。 Here is a sample code of mine, you should grab the idea:这是我的一个示例代码,你应该抓住这个想法:

def transconum(morph):
    if (morph == 'S'):
        return 1.0
    else:
        return 0.0

CompactGroups['MorphNum'] = CompactGroups['MorphGal'].apply(transconum)

Second solution第二种解决方案

Another way would be to overwrite labels on the flight.另一种方法是覆盖航班上的标签。 Here is a sample code of mine which works perfectly:这是我的一个示例代码,它完美运行:

grid = sns.jointplot(x="MorphNum", y="PropS", data=CompactGroups, kind="reg")
grid.set_axis_labels("Central type", "Spiral proportion among satellites")
grid.ax_joint.set_xticks([0, 1, 1])
plt.xticks(range(2), ('$Red$', '$S$')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM