使用标记笔进行注释,而不是箭头。

3
怎样使用这段代码:
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

并且创建一个圆形标记(点),而不是箭头。尽管如此,我想保留箭头的文本。

1个回答

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

结果: 这里输入图片描述

有没有办法让点更大一些?谢谢,那正是我想要的。 - Artur Müller Romanov
1
使用 ms 关键字,例如 ax.plot(xmax, ymax, 'ro', ms=20) 来定义标记大小。 - MPA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接