2017-05-28 110 views
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看到,頂部右側框,傳說來了爲0。如何添加的東西在那邊有意義?

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