2017-05-05 92 views
2

我有一個熊貓數據框有3個類和n個特徵的數據點。大熊貓分散矩陣的傳說

以下代碼會生成一個散點圖矩陣,其中包含數據框中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() 

這樣的:

Scatter matrix of this dataframe

我遇到的問題是,plt.legend()似乎不工作,它完全不顯示圖例(或它的微小'le8'在第二行的第一列中幾乎看不到......)

我想要的是一個只顯示哪種顏色是哪個類的傳說。

我試過所有建議的問題,但沒有解決方案。 我也試圖把標籤圖例中的功能參數如下:

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

但無濟於事..

我在做什麼錯?

+0

我從來沒有使用過'pd.scatter_matrix'繪製散佈矩陣圖,但seaborn可能是有用的。這裏有一個圖例的例子:https://seaborn.pydata.org/examples/scatterplot_matrix.html –

回答

3

大熊貓scatter_matrix是幾個matplotlib scatter圖的包裝。參數傳遞給scatter函數。但是,散點通常意味着與顏色貼圖一起使用,而不是帶有離散標記點的圖例,因此沒有可用的參數來自動創建圖例。我很害怕你不得不手動創建圖例。爲此,您可以使用matplotlib的plot函數(使用空數據)在scatter中創建點,並將它們作爲句柄添加到圖例中。如果你想

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

+0

是的,thx !,這個伎倆! –