pandas 散布矩阵的熊猫图例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43801637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:32:31  来源:igfitidea点击:

Pandas legend for scatter matrix

pythonpandasmatplotlib

提问by NG.

I have a pandas dataframe with 3 classes and datapoints of n features.

我有一个包含 3 个类和 n 个特征的数据点的 Pandas 数据框。

The following code produces a scatter matrix with histograms in the diagonal, of 4 of the features in the dataframe.

以下代码生成一个散点矩阵,在对角线上有直方图,数据帧中有 4 个特征。

colums = ['n1','n2','n3','n4']
grr = pd.scatter_matrix(
dataframe[columns], c=y_train, figsize=(15,15), label=['B','N','O'], marker='.',
    hist_kwds={'bins':20}, s=10, alpha=.8, cmap='brg')
plt.legend()
plt.show()

like this:

像这样:

Scatter matrix of this dataframe

此数据框的散点矩阵

The problem I'm having is that plt.legend() doesn't seem to work, it shown no legend at all (or it's the tiny 'le8' barely visible in the first column of the second row...)

我遇到的问题是 plt.legend() 似乎不起作用,它根本没有显示任何图例(或者它是第二行第一列中几乎看不到的小“le8”......)

What I'd like to have is a single legend that just shows which color is which class.

我想要的是一个图例,它只显示哪个颜色是哪个类。

I've tried all the suggested questions but none have a solution. I also tried to put the labels in the legend function parameters like this:

我已经尝试了所有建议的问题,但没有一个解决方案。我还尝试将标签放在图例函数参数中,如下所示:

plt.legend(label=['B','N','O'], loc=1)

but to no avail..

但无济于事..

What am I doing wrong?

我究竟做错了什么?

采纳答案by ImportanceOfBeingErnest

The pandas scatter_matrixis a wrapper for several matplotlib scatterplots. Arguments are passed on to the scatterfunction. However, the scatter is usually meant to be used with a colormap and not a legend with discrete labeled points, so there is no argument available to create a legend automatically.

pandasscatter_matrix是几个 matplotlibscatter图的包装器。参数传递给scatter函数。但是,散点通常用于颜色图而不是具有离散标记点的图例,因此没有可用于自动创建图例的参数。

I'm affraid you have to manually create the legend. To this end you may create the dots from the scatter using matplotlib's plotfunction (with empty data) and add them as handles to the legend.

我恐怕您必须手动创建图例。为此,您可以使用 matplotlib 的plot函数(使用空数据)从散点图创建点,并将它们添加为图例的句柄。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.subplot.right"] = 0.8

v= np.random.rayleigh(size=(30,5))
v[:,4] = np.random.randint(1,4,size=30)/3.
dataframe= pd.DataFrame(v, columns=['n1','n2','n3','n4',"c"])

columns = ['n1','n2','n3','n4']
grr = pd.scatter_matrix(
dataframe[columns], c=dataframe["c"], figsize=(7,5), label=['B','N','O'], marker='.',
    hist_kwds={'bins':20}, s=10, alpha=.8, cmap='brg')

handles = [plt.plot([],[],color=plt.cm.brg(i/2.), ls="", marker=".", \
                    markersize=np.sqrt(10))[0] for i in range(3)]
labels=["Label A", "Label B", "Label C"]
plt.legend(handles, labels, loc=(1.02,0))
plt.show()

enter image description here

在此处输入图片说明