用标记而不是箭头进行注释

Art*_*nov 3 python matplotlib plot-annotations

我该如何获取这段代码:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

arrow = ax.annotate('local max:' + str(ymax), xy=(xmax, ymax), xytext=(xmax, ymax + 2),
            arrowprops=dict(arrowstyle = '-', connectionstyle = 'arc3',facecolor='red'))

#==============================================================================
# arrow.remove()
#==============================================================================

ax.set_ylim(0,20)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

并创建一个圆形标记(点)而不是箭头。我想保留箭头的文字。

MPA*_*MPA 5

如果您只想要一个点和文本,没有连接到点的箭头/线,您可以使用以下函数简单地执行此text操作:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

# Add dot and corresponding text
ax.plot(xmax, ymax, 'ro')
ax.text(xmax, ymax+2, 'local max:' + str(ymax))

ax.set_ylim(0,20)
plt.show()
Run Code Online (Sandbox Code Playgroud)

结果: 在此输入图像描述