2016-08-21 11 views
1

私のコードに問題があります。 1つのグラフのデータのみを表示し、他のグラフは空白です。なぜそれが動作していないのかわかりません。
私はsubplot()関数を使用しています。私の推測では、その理由が私の関数のフォーマット方法かもしれないということです。グラフは1つのグラフにのみデータを表示しますが、他のグラフは表示しません

import numpy as np 
import matplotlib.pyplot as plt 
import cvxopt as opt 
from cvxopt import blas, solvers 
import pandas as pd 
import mpld3 
from mpld3 import plugins 

np.random.seed(123) 
solvers.options['show_progress'] = False 

n_assets = 4 

n_obs = 1000 # original 1000 

return_vec = np.random.randn(n_assets, n_obs) 

def rand_weights(n): 
    k = np.random.rand(n) 
    return k/sum(k) print(rand_weights(n_assets)) print(rand_weights(n_assets)) 

def random_portfolio(returns): 
    p = np.asmatrix(np.mean(returns,axis=1)) 
    w = np.asmatrix(rand_weights(returns.shape[0])) 
    C = np.asmatrix(np.cov(returns)) 

    mu = w * p.T 
    sigma = np.sqrt(w * C * w.T) 

    #this recursion reduces outlier to keep the graph nice 
    if sigma > 2: 
     return random_portfolio(returns) 
    return mu, sigma 

n_portfolios = 500 

means, stds = np.column_stack([random_portfolio(return_vec) for _ in range(n_portfolios)]) 


plt.plot(return_vec.T, alpha=.4); 
plt.xlabel('time') 
plt.ylabel('returns') 
plt.figure(1) 
plt.subplot(212) 

plt.plot(stds, means, 'o', markersize = 5) 
plt.xlabel('std') 
plt.ylabel('mean') 
plt.title('Mean and standard deviation of returns of randomly generated portfolios') 
plt.subplot(211) 
plt.figure(1) 

plt.show() 

my graph

答えて

2

あなたはplt.plotにあなたの最初の呼び出しの前の行plt.subplot(211)を移動する必要があります。これは、plt.subplotへの呼び出しが、そのサブプロットの実際のプロットに先行する必要があるからです。

+0

それは魅力的なように機能し、私はそれを逃したとは思えません。ありがとう!! – Cesar

+0

うまくいけば、おそらく答えを受け入れるべきです。 – bpachev

関連する問題