如何向散点图添加图例 [英] How to add a legend to scatterplot

查看:270
本文介绍了如何向散点图添加图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做机器学习课程的练习.我将矩阵形式的图像数据集附加到矩阵中,然后添加到数据矩阵中,然后对其进行标准化,然后计算出主要成分.Labels是一个数组,其中包含每个图像的标签(包含标签的子目录)我需要可视化成对的主成分,在这部分是前两个.这位教授的建议是使用matplotli.scatter函数,我发现seaborn.scatterplot函数看起来更好,但是在没有两个函数的情况下,我都设法在图例上添加了标签名称.

pca = PCA()X_t = pca.fit_transform(数据矩阵)X_r = pca.inverse_transform(X_t)plt.figure(figsize =(25,5))颜色 = ['r','g','b','p']plt.subplot(1、3、1)sns.scatterplot(X_t [:,0],X_t [:,1],色相=标签,调色板=颜色,图例='满')plt.title('PC 1 和 2')

我是 Python 和机器学习库的新手

如建议的那样,我尝试修改了鳕鱼:

data = {"x" : X_t[:,0], "y" : X_t[:,1], "label" : 标签}sns.scatterplot(x=x",y=y",hue=标签",调色板=颜色,数据=数据,图例=全")

但是我得到了相同的结果:我有图例,但没有标签的名称

要通过matplotlib的 scatter 实现相同的功能,您需要自己创建图例,这确实有点麻烦,但可能有助于理解.

将 numpy 导入为 np导入matplotlib.pyplot作为plt数据= {"x":np.random.rand(10),"y":np.random.rand(10),"label":np.random.choice(["Label 1","Label 2"],size = 10)}标签, inv = np.unique(data["label"], return_inverse=True)散点= plt.scatter(x ="x",y ="y",c = inv,data = data)句柄 = [plt.Line2D([],[],marker="o", ls="",color=scatter.cmap(scatter.norm(yi))) for yi in np.unique(inv)]plt.legend(句柄,标签)plt.show()

另请参见将图例添加到散点图(PCA)

I am doing an exercise for a Machine Learning course. I appended to a matrix a dataset of images in form of arrays into datamatrix, then I standardized it and then computed the principal components. Labels is an array containing for each image the label (that was the subdirectory containing it) I need to visualize pairs of principal components, in this part the first two. The suggestion from the professor was to use the matplotli.scatter function, I found the seaborn.scatterplot function that seems better, but with none of the two I managed to put a legend with the labels names onto it.

pca = PCA()
X_t = pca.fit_transform(datamatrix)
X_r = pca.inverse_transform(X_t)

plt.figure(figsize=(25,5))

colours = ['r','g','b','p']
plt.subplot(1, 3, 1)
sns.scatterplot(X_t[:,0], X_t[:,1], hue=labels, palette=colours, legend='full')
plt.title('PC 1 and 2')

I am new to Python and to Machine Learnings libaries

Edit: As suggested I tried modifying the cod:

data = {"x" : X_t[:,0], "y" : X_t[:,1], "label" : labels}
sns.scatterplot(x="x", y="y", hue="label", palette=colours, data=data, legend='full')

But I obtain the same result: I have the legend, but without the name of the labels capture

解决方案

Seaborn scatterplot will automatically create a legend as shown in the second example from the documentation. It does however require to have the data in a dictionary-like structure, which is common for pandas dataframes.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

data = {"x" : np.random.rand(10),
        "y" : np.random.rand(10),
        "label" : np.random.choice(["Label 1", "Label 2"], size=10)}

sns.scatterplot(x="x", y="y", hue="label", data=data)
plt.show()

To achieve the same via matplotlib's scatter you would need to create the legend yourself, which is indeed a bit more cumbersome, but may be helpful for understanding.

import numpy as np
import matplotlib.pyplot as plt

data = {"x" : np.random.rand(10),
        "y" : np.random.rand(10),
        "label" : np.random.choice(["Label 1", "Label 2"], size=10)}

labels, inv = np.unique(data["label"], return_inverse=True)
scatter = plt.scatter(x="x", y="y", c = inv, data=data)

handles = [plt.Line2D([],[],marker="o", ls="", 
                      color=scatter.cmap(scatter.norm(yi))) for yi in np.unique(inv)]
plt.legend(handles, labels)

plt.show()

Also see Add legend to scatter plot (PCA)

这篇关于如何向散点图添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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