2016-07-07 3 views
1

몇 가지 주식 정보로 DataFrame을 만들었습니다. 인덱스를 데이터 레이블로 사용하려고하면 작동하지 않습니다. 이것은 주식을 차별화 할 수 없도록 만든다. 범례를 플롯하면 인덱스 목록, dtype 및 이름이 표시됩니다. 모든 포인트를 하나의 레이블로 결합하는 것 같습니다.분산 형 플롯의 색인 사용 전설

내 테이블 :

  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 루프를 사용하여 단일 플롯에서 개별 티커 행을 그릴 수 있고 고유 색 (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

관련 문제