1
votes

I've created a DataFrame with some stock information. When I try to use the index as the data labels it does not work. This makes it so I cannot differentiate the stocks, especially when I add more. When I plot the legend it shows the index list, dtype, and name. It seems to be combining every point into one label.

My Table:

         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

My plotting code:

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

My legend output:

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

1 Answers

1
votes

I could be wrong but I think label=df.index will not work when your index is unique text labels you want to plot individually. If your index is unique (or if it's a groupby), you can use a for loop to plot individual ticker rows in a single plot as well as give them a unique color (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()

Result:

enter image description here