Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为一个数据帧调整带有多个标签的legend Seaborn jointplot_Python_Plot_Seaborn_Legend_Jointplot - Fatal编程技术网

Python 为一个数据帧调整带有多个标签的legend Seaborn jointplot

Python 为一个数据帧调整带有多个标签的legend Seaborn jointplot,python,plot,seaborn,legend,jointplot,Python,Plot,Seaborn,Legend,Jointplot,假设我有一个具有以下结构的数据帧: Column 1 Column2 Column3 x1 y1 0 x2 y2 0 x3 y3 1 x4 y4 1 x5 y5 1 .... x_n-1 y_n-1 5 x_n y_n 5 我想创建一个jointplot,在这里我根据Column3的值指定不同

假设我有一个具有以下结构的数据帧:

Column 1   Column2 Column3
x1         y1       0
x2         y2       0
x3         y3       1
x4         y4       1
x5         y5       1
          ....
x_n-1      y_n-1    5
x_n        y_n      5
我想创建一个jointplot,在这里我根据Column3的值指定不同的颜色。我使用的命令是

h = sns.jointplot(x="Column1", y="Column2", data=data, hue="Column3")
所以我所有的点都有6种不同的颜色。从上一个命令生成的图例具有标签“0”、“1”。。。“5”,这不是解释性的。我想要的是“label0”、“label1”等等,而不是它们

我尝试使用以下命令:

h.ax_joint.legend([data.loc[data['Column3'] == 0], data.loc[data['Column3'] == 1], data.loc[data['Column3'] == 2], data.loc[data['Column3'] == 3], data.loc[data['Column3'] == 4], data.loc[data['Column3'] == 5]], ['label0', 'label1', 'label2', 'label3', 'label4', 'label5'])
但执行它时,我有以下信息:

A proxy artist may be used instead. See: https://matplotlib.org/users/legend_guide.html#creating-artists-specifically-for-adding-to-the-legend-aka-proxy-artists

当然,它不再描绘任何传奇。我一直在查看建议的文档,但我不知道如何改进它。有人有主意吗?提前谢谢

最简单、最符合Seaborn精神的方法是(暂时)重命名色调列的标签:

导入seaborn作为sns
作为pd进口熊猫
将numpy作为np导入
data=pd.DataFrame({“Column1”:np.random.randn(36)*10,
“第二栏”:名词阿兰奇(36)%6+名词随机数randn(36)/4,
“第三栏”:名词阿兰奇(36)%6})
标签=['label0'、'label1'、'label2'、'label3'、'label4'、'label5']
g=sns.jointplot(data=data.replace({“Column3”:{i:label for i,label in enumerate(labels)}}),
x=“Column1”,y=“Column2”,hue=“Column3”,palete=“turbo”)
g、 ax_接头。反向_yaxis()

另一个选项是再次创建图例并提供新标签。第二个图例将替换默认图例。如果您还希望更改其他属性(例如图例的位置)或删除其标题,则此选项非常有用:

g=sns.jointplot(x=“Column1”,y=“Column2”,data=data,hue=“Column3”,palete=“turbo”)
句柄,标签=g.ax\U关节。获取\U图例\U句柄\U标签()
g、 ax_接头图例(手柄=手柄,标签=['label0'、'label1'、'label2'、'label3'、'label4'、'label5'],
title=“Column3”)