Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotate with marker instead of arrow

How do I take this code:

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

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

like image 393
Artur Müller Romanov Avatar asked Sep 21 '25 03:09

Artur Müller Romanov


1 Answers

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

Result: enter image description here

like image 136
MPA Avatar answered Sep 22 '25 17:09

MPA