In my plot, I am trying to annotate with multiple values at each point.
weights = [np.array([w, 1-w]) for w in np.linspace(0, 1, 5)]
mu = [0.5, 0.25]
def portfolio_return(weights, returns):
return weights.T @ returns
rets = [portfolio_return(w, mu) for w in weights]
S = [[0.493, 0.11], [0.11, 0.16]]
def portfolio_vol(weights, cov):
return (weights.T @ cov @ weights)**0.5
vols = [portfolio_vol(w, S) for w in weights]
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
markers_on = [1, 3]
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(vols, rets, 'g-')
for marker in markers_on:
plt.plot(vols[marker], rets[marker], 'bs')
w1, w2 = weights[marker][0], weights[marker][1]
ax.annotate(f'w = ({w1:.1f}, {w2:.1f})', (w1, w2), xy=(vols[marker]+.08, rets[marker]-.03))
plt.xlabel('Risk')
plt.ylabel('Return')
plt.show()
This returns error -
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-0e29da296b76> in <module>
11 plt.plot(vols[marker], rets[marker], 'bs')
12 w1, w2 = weights[marker][0], weights[marker][1]
---> 13 ax.annotate(f'w = ({w1:.1f}, {w2:.1f})', (w1, w2), xy=(vols[marker]+.08, rets[marker]-.03))
TypeError: annotate() got multiple values for argument 'xy'
New to Python plot. All I am trying to do is annotate two points on the plot, in each showing both the weights.
---------------------------------------------------------------------------
Note - made the following changes following @ewong's comment.
for marker in markers_on:
plt.plot(vols[marker], rets[marker], 'bs')
w1, w2 = weights[marker][0], weights[marker][1]
ax.annotate(r'w = ({w1:%.2f}, {w2:%.2f})', (w1, w2))
No error, which is good. Unfortunately, although it marks two positions, does not show the weights.
Also there is a significant amount of white space before the plot shows up. I have to scroll down in jupyter notebook.
----------------------------------------------------------------------------------
Further changes made. Getting the plot with all three marker. But not the weights.
for marker in markers_on:
plt.plot(vols[marker], rets[marker], 'bs')
w1, w2 = weights[marker][0], weights[marker][1]
text = f'w = ({w1:.2f}, {w2:.2f})', (w1, w2)
ax.annotate(s = text, xy=(vols[marker]+.08, rets[marker]-.03))