2017-05-28 7 views
0

私はパンダプロットを使って密度グラフをプロットしています。しかし、グラフごとに適切な凡例を追加することはできません。私のコードと結果はとして以下の通りです: - 0はどのように私はあそこに意味のある何かを追加するようパンダプロットでの伝説の追加

for i in tickers: 
    df = pd.DataFrame(dic_2[i]) 
    mean=np.average(dic_2[i]) 
    std=np.std(dic_2[i]) 
    maximum=np.max(dic_2[i]) 
    minimum=np.min(dic_2[i]) 
    df1=pd.DataFrame(np.random.normal(loc=mean,scale=std,size=len(dic_2[i]))) 
    ax=df.plot(kind='density', title='Returns Density Plot for '+ str(i),colormap='Reds_r') 
    df1.plot(ax=ax,kind='density',colormap='Blues_r') 

enter image description here

あなたは、PICに右上のボックスを見ることができ、伝説が来ていますか?

print(df.head()) 
      0 
0 -0.019043 
1 -0.0212065 
2 0.0060413 
3 0.0229895 
4 -0.0189266 
+0

あなたは、サンプルデータを投稿することができますか? –

+0

@AndrewL - dfにはインデックスの毎日の返品データしかありません。私はdf.head()で上で編集しています –

答えて

2

グラフを作成した方法を再構成したいと思うかもしれません。これを行う簡単な方法は、プロットする前にaxを作成することです:

# sample data 
df = pd.DataFrame() 
df['returns_a'] = [x for x in np.random.randn(100)] 
df['returns_b'] = [x for x in np.random.randn(100)] 
print(df.head()) 
    returns_a returns_b 
0 1.110042 -0.111122 
1 -0.045298 -0.140299 
2 -0.394844 1.011648 
3 0.296254 -0.027588 
4 0.603935 1.382290 

fig, ax = plt.subplots() 

私は、あなたの変数で指定されたパラメータを使用してデータフレームを作成しました:

mean=np.average(df.returns_a) 
std=np.std(df.returns_a) 
maximum=np.max(df.returns_a) 
minimum=np.min(df.returns_a) 

pd.DataFrame(np.random.normal(loc=mean,scale=std,size=len(df.returns_a))).rename(columns={0: 'std_normal'}).plot(kind='density',colormap='Blues_r', ax=ax) 
df.plot('returns_a', kind='density', ax=ax) 

あなたが作業しているこの第二のデータフレームがされ既定で列0で作成されます。これをリネームする必要があります。

enter image description here

0

私はこれを行うための簡単な方法を考え出しました。データフレームに列名を追加するだけです。

for i in tickers: 
    df = pd.DataFrame(dic_2[i],columns=['Empirical PDF']) 
    print(df.head()) 
    mean=np.average(dic_2[i]) 
    std=np.std(dic_2[i]) 
    maximum=np.max(dic_2[i]) 
    minimum=np.min(dic_2[i]) 
    df1=pd.DataFrame(np.random.normal(loc=mean,scale=std,size=len(dic_2[i])),columns=['Normal PDF']) 
    ax=df.plot(kind='density', title='Returns Density Plot for '+ str(i),colormap='Reds_r') 
    df1.plot(ax=ax,kind='density',colormap='Blues_r') 

enter image description here