4

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?

5
  • I don't quite follow your 3rd point, do you mean you want to specify the size of the saved image, or to crop the axis off, or something else? Commented May 8, 2012 at 14:28
  • Isn't this matplotlib.sourceforge.net/api/… what you want ? Commented May 8, 2012 at 15:10
  • In my specific case, I want it (intensity projection of a source image) to fit an 800*600 space, just below the 800*600 source image Commented May 8, 2012 at 15:42
  • savefig, takes figsize, and dpi arguments. So you can set the size no probs, also you'll probably want to turn the axes off. Commented May 8, 2012 at 15:56
  • This would introduce scaling artifacts and might look extremely ugly. Commented May 8, 2012 at 16:05

1 Answer 1

7

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.