Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using matplotlib to annotate certain points

While I can hack together code to draw an XY plot, I want some additional stuff:

  • Vertical lines that extend from the X axis to a specified distance upward
  • text to annotate that point, proximity is a must (see the red text)
  • the graph to be self-contained image: a 800-long sequence should occupy 800 pixels in width (I want it to align with a particular image as it is an intensity plot)

enter image description here

How do I make such a graph in mathplotlib?

like image 764
Jesvin Jose Avatar asked Dec 20 '22 22:12

Jesvin Jose


1 Answers

You can do it like this:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
data = (0, 2, 3, 5, 5, 5, 9, 7, 8, 6, 6)
ax.plot(data, 'r-', linewidth=4)

plt.axvline(x=5, ymin=0, ymax=4.0 / max(data), linewidth=4)
plt.text(5, 4, 'your text here')
plt.show()

Note, that somewhat strangely the ymin and ymax values run from 0 to 1, so require normalising to the axis

enter image description here


EDIT: The OP has modified the code to make it more OO:

fig = plt.figure()
data = (0, 2, 3, 5, 5, 5, 9, 7, 8, 6, 6)

ax = fig.add_subplot(1, 1, 1)
ax.plot(data, 'r-', linewidth=4)
ax.axvline(x=5, ymin=0, ymax=4.0 / max(data), linewidth=4)
ax.text(5, 4, 'your text here')
fig.show()
like image 123
fraxel Avatar answered Dec 23 '22 12:12

fraxel