2016-07-07 7 views
1

在庫情報を含むDataFrameを作成しました。インデックスをデータラベルとして使用しようとすると、インデックスが機能しません。これは、私が株式を区別することができないようにする、特に私が追加する場合。凡例をプロットすると、インデックスリスト、dtype、および名前が表示されます。すべてのポイントを1つのラベルにまとめるようです。散布図の索引を使用する凡例

マイ表:

  Dividend ExpenseRatio Net_Assets PriceEarnings PriceSales 
Ticker 
ijr  0.0142   0.12  18.0   20.17  1.05 
ijh  0.0159   0.12  27.5   20.99  1.20 

マイプロットコード:

plt.scatter(df.PriceSales, df.PriceEarnings, label = df.index) 
plt.xlabel('PriceSales') 
plt.ylabel('PriceEarnings') 
plt.legend() 
plt.show() 

私の伝説出力:

Index(['ijr', 'ijh'],dtype='object',name='Ticker') 

答えて

1

私が間違っている可能性が、私はlabel=df.indexが動作しません考えるとき、あなたのインデックス個別にプロットする独自のテキストラベルです。インデックスがユニーク(またはグループバイの場合)の場合は、forループを使用して、個々のティッカー行を1つのプロットにプロットし、固有の色(c=np.random.rand(3,1))を付けることができます。

import pandas as pd 
import numpy as np 
import matplotlib.pyplot as plt 

df = pd.DataFrame({'Dividend': [0.0142, 0.0159], 'ExpenseRatio': [0.12, 0.12], 
        'Net_Assets': [18.0, 27.5], 'PriceEarnings': [20.17, 20.99], 
        'PriceSales': [1.05, 1.2]}, 
        columns=['Dividend', 'ExpenseRatio', 'Net_Assets', 'PriceEarnings', 'PriceSales'], 
        index=['ijr', 'ijh']) 

df.index.name = 'Ticker' 

for ticker,row in df.iterrows(): 
    plt.scatter(row['PriceSales'], row['PriceEarnings'], label=ticker, c=np.random.rand(3,1), s=100) 

plt.xlabel('PriceSales') 
plt.ylabel('PriceEarnings') 
plt.legend() 
plt.show() 

結果:あなたのソリューションが働い

enter image description here

+0

!どうもありがとうございました。 – Evy555

関連する問題