14

If I make a graph using pylab from matlotlib like so...

import pylab as p
x = [0,1,2]
y = [2,4,6]
p.plot(x,y)
p.show()

I want to use the p.text function to add text to the graph. However, I want to put the text in the margin outside of the data window. The text function only accepts x,y coordinates that correspond to the data points rather than absolute x,y pixels coordinates of the entire window. Any idea how I can write text in the margins?

2 Answers 2

27

You can use the figtext function. Only note that the coordinates are 0-1, so something like the following places text to the left of the vertical axis:

p.figtext(0.05, 0.5, 'foo')

See the linked docs for more information.

1
  • Hi, what are the coordinates "relative" to? Where could I look up what they mean (the figtext document doesn't give me a link to any documentation that will tell me - presumably this is documented somewhere but I am finding it very hard to find). May 29, 2015 at 23:00
3

If you're not using the pyplot mode and instead want to do this in an object-orientated method you can do the following:

text.set_transform(fig.transFigure)

Where text is a matplotlib.text.Text object and fig is a matplotlib.figure.Figure object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.