Python plot error : annotate() got multiple values for argument 'xy'

Python plot error : annotate() got multiple values for argument 'xy'

在我的情节中,我试图在每个点用多个值进行注释。

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()

这个returns错误-

---------------------------------------------------------------------------
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'

Python 情节的新内容。我想要做的就是在图上注释两个点,每个点都显示两个权重。

---------------------------------------------------------------------------

注意 - 根据@ewong 的评论进行了以下更改。

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))

没有错误,很好。不幸的是,虽然它标记了两个位置,但没有显示权重。

在情节出现之前还有大量的白色 space。我必须在 jupyter notebook 中向下滚动。

----------------------------------------------------------------------------------

进行了进一步的更改。使用所有三个标记获取情节。但不是权重。

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))

之所以没有看到粗细是因为你用xy=(vols[marker]+.08, rets[marker]-.03)调整了文字的位置。从你的图片中,我注意到 x max 是 0.02250.08 大于 0.0225,所以文本越界。

import numpy as np
import matplotlib.pyplot as plt

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]


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]
    text = f'w = ({w1:.1f}, {w2:.1f})'
    ax.annotate(text, xy=(vols[marker]+.005, rets[marker]-0.005))

plt.xlabel('Risk')
plt.ylabel('Return')
plt.show()