2017-02-25 30 views
1

我目前正在嘗試爲某些資產(特定於某些資產)獲取的對數回報構建相對頻率圖,其中我使用了plt.yscale在y軸上的log10標度( '登錄')。但是,我得到了一些垂直線條清楚地趨向於無窮大,在下面顯示我的圖,我在Python中構建的某些點:如何擺脫相對頻率圖後出現的垂直線? (Python 3.5)

這顯然是不應該的。相反,它應該是這樣的曲線圖:

這顯然是與我相似,不同之處在於它不包括那些點的垂直線。我的代碼如下:

plt.figure(1) 
plt.figure(figsize=(9,7)) 
hist1, bins1 = np.histogram(returns_assetA_daily_mat, bins=20) 
hist2, bins2 = np.histogram(returns_assetA_weekly_mat, bins=20) 
hist3, bins3 = np.histogram(returns_assetA_monthly_mat, bins=20) 
hist1 = hist1/len(returns_assetA_daily_mat) 
hist2 = hist2/len(returns_assetA_weekly_mat) 
hist3 = hist3/len(returns_assetA_monthly_mat) 
bins1 = 0.5 * (bins1[1:] + bins1[:-1]) 
bins2 = 0.5 * (bins2[1:] + bins2[:-1]) 
bins3 = 0.5 * (bins3[1:] + bins3[:-1]) 
plt.plot(bins1, hist1, bins2, hist2, bins3, hist3) 
plt.yscale('log') 
plt.xlabel('Log-Returns') 
plt.ylabel('Relative Frequency') 
plt.title('Original for Asset A') 
plt.show() 

這是相當有用知道

returns_assetA_daily_mat, returns_assetA_weekly_mat, returns_assetA_monthly_mat 

只是排陣列,其中包含負值資產的每日,每週和每月的對數收益值,正值和零也是如此,所以也許因爲我在y軸上做log10尺度,負值或零點可能是潛在問題的原因,因爲顯然當x趨於零時對數將趨於負無窮?也許問題在於我的代碼結構?如果沒有解決這個問題的方法,我有什麼辦法可以將包含垂直線的點趨向於無窮大,以便它們看起來像是孤立點?我是一名Python新手,目前正在學習它作爲計算金融碩士學位的一部分,所以任何形式的幫助都將受到高度讚賞!提前感謝!

回答

0

在Matplotlib中,在圖中直觀地「刪除」點的一種方法是將受影響的點設置爲numpy.nan。這樣做的效果是,numpy.nan之前和之後的點會顯示出我認爲你之後的差距。

因此,對於您的陣列,請在繪圖之前查找任何值爲負數或0並將它們設置爲numpy.nan。因爲你正在計算直方圖,所以這些值不應該產生負值,所以你只能檢查等於0的bin。

您需要確保的一件事是將陣列的類型更改爲floatnumpy.nan僅適用於浮點數組。

如果你也想畫出他們每增加一個傳說,只需調用matplotlib.pyplot.plot三次,然後添加你的傳奇繪製在同一人物時每個陣列一個,然後顯示情節:

plt.figure(1) 
plt.figure(figsize=(9,7)) 
hist1, bins1 = np.histogram(returns_assetA_daily_mat, bins=20) 
hist2, bins2 = np.histogram(returns_assetA_weekly_mat, bins=20) 
hist3, bins3 = np.histogram(returns_assetA_monthly_mat, bins=20) 
hist1 = hist1/len(returns_assetA_daily_mat) 
hist2 = hist2/len(returns_assetA_weekly_mat) 
hist3 = hist3/len(returns_assetA_monthly_mat) 
bins1 = 0.5 * (bins1[1:] + bins1[:-1]) 
bins2 = 0.5 * (bins2[1:] + bins2[:-1]) 
bins3 = 0.5 * (bins3[1:] + bins3[:-1]) 

# New code 
hist1_new = hist1.astype(np.float) 
hist2_new = hist2.astype(np.float) 
hist3_new = hist3.astype(np.float) 
hist1_new[hist1 <= 0] = np.nan 
hist2_new[hist2 <= 0] = np.nan 
hist3_new[hist3 <= 0] = np.nan 

# New - Plot the three graphs separately for making the legend 
# Also plot the NaN versions 
plt.plot(bins1, hist1_new, label='Daily') 
plt.plot(bins2, hist2_new, label='Weekly') 
plt.plot(bins3, hist3_new, label='Monthly') 

plt.yscale('log') 
plt.xlabel('Log-Returns') 
plt.ylabel('Relative Frequency') 
plt.title('Original for Asset A') 
plt.legend() # Added for the legend 
plt.show() 

我沒有你的數據,但我可以告訴你這個工作的一個玩具例子:

# Import relevant packages 
import numpy as np 
import matplotlib.pyplot as plt 

# Create array from 0 to 8 for the horizontal axis 
x = np.arange(9) 

# Create test array with some zero, positive and negative values 
y = np.array([1, 2, 3, 0, -1, -2, 1, 2, -1]) 

# Create a figure with two graphs in one row 
plt.subplot(1, 2, 1) 

# Graph the data normally 
plt.plot(x, y) 

# Visually remove those points that are zero or negative 
y2 = y.astype(np.float) 
y2[y2 <= 0] = np.nan 

# Plot these points now 
plt.subplot(1, 2, 2) 
plt.plot(x, y2) 

# Adjust the x and y limits (see further discussion below) 
plt.xlim(0, 8) 
plt.ylim(-1, 3) 

# Show the figure 
plt.show() 

注意,第二個情節我執行了xy限制與第一個繪圖相同,因爲我們在視覺上刪除了這些點,所以軸將自動調整。我們得到:

enter image description here

+0

非常感謝!這非常有幫助,我很高興地說它絕對有效! – Jayjay95

+0

@ Jayjay95啊哈,非常好:)想知道它是否。讓我知道你是否需要其他東西,我很樂意修改。如果沒有別的,請考慮接受我的回答,讓社區知道你在這件事上不再需要幫助。祝你好運! – rayryeng

+0

還有一個問題,這可能更加微不足道:因爲我在plt.plot()函數中輸入了三個不同的圖表,我該如何去添加三個單獨的標籤(每日,每週,每月將是標籤的名稱)爲了添加圖例的圖例?因爲目前如果我添加一個標籤名稱,如「每日」,則該名稱將被分配給所有三個不同的地塊,而不管該地塊是否屬於每月或每日數據。再次感謝! :) – Jayjay95