Matplotlib用标记而不是箭头注释 [英] Matplotlib annotate with marker instead of arrow

查看:34
本文介绍了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()

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

and create a circular marker (Point) instead of an arrow. I would like to Keep the text of the arrow though.

推荐答案

如果您只想要一个点和没有箭头/线连接到该点的文本,您可以简单地使用 text功能:

If you only want a dot and text with no arrow/line connecting to the dot, you can simply do so using the text function:

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

结果:

这篇关于Matplotlib用标记而不是箭头注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆