for i in range(5000):
weights = np.random.random(12)
weights /= np.sum(weights)
ER = np.sum(log_returns.mean() * weights) * 252
SD = np.sqrt(np.dot(weights.T , np.dot(an_cov,weights)))
rf = 0.02
Sharpe = (ER - rf)/SD
port_return.append(ER)
port_vol.append(SD)
port_sharpe.append(Sharpe)
s_weight.append(weights)
P = {'Portfolio Expected Return':port_return, 'Portfolio Standard
Deviation':port_vol, 'Stock Weights':s_weight , 'Portfolio Sharpe
Ratio':port_sharpe}
Q = pd.DataFrame(P)
plt.style.use('seaborn')
Q.plot.scatter(x='Portfolio Standard Deviation',y='Portfolio Expected
Return',c = 'Portfolio Sharpe Ratio',figsize=(12,9), grid=True,
cmap='RdYlGn')
plt.xlabel('Volatility (Std. Deviation)')
plt.ylabel('Expected Returns')
plt.title('random portfolios')
plt.show()
我尝试多次绘制此图,但是每次X轴为何丢失?色标标签,Y轴和标题正常,但只有X轴不起作用。