Matplotlib用标记而不是箭头注释

时间:2018-04-23 15:03:38

标签: python matplotlib

如何使用此代码:

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

enter image description here

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

1 个答案:

答案 0 :(得分:1)

如果您只想要一个没有箭头/线连接到点的点和文本,您可以使用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()

结果: enter image description here