1

I am new to Python and I am actually trying to plot a figure over a subplot.

The difficult thing is that I need the axes property which is a string I can obtain by simply printing the subplot (Example below).

figure(1)
a = subplot(222)
print a
Axes(xpos,ypos;deltaxxdeltay)

This string contains all the information I need for what I want to do (a simple axes([x, y , deltax, deltay]). But unfortunately, I would need to redirect the output of print() to a variable that I can parse after (with re()).

Does anyone have an idea of how to do it (I only need the output of this string, other printed values in the program must not be affected)?

1 Answer 1

2

Instead of going via a string, you can access that information directly, which I think is cleaner:

>>> print a
Axes(0.547727,0.536364;0.352273x0.363636)
>>> a._position.bounds
(0.54772727272727262, 0.53636363636363638, 0.35227272727272729, 0.36363636363636365)
>>> a._position.bounds[3]
0.36363636363636365

Although you could have the string if you like:

>>> str(a)
'Axes(0.547727,0.536364;0.352273x0.363636)'
>>> str(a)[5:-1]
'0.547727,0.536364;0.352273x0.363636'

I use the IPython interpreter, so it was easy to figure out where the information was coming from by looking at the source for a.__str__:

>>> a.__str__??
Type:       instancemethod
String Form:<bound method AxesSubplot.__str__ of <matplotlib.axes.AxesSubplot object at 0x103e187d0>>
File:       /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py
Definition: a.__str__(self)
Source:
    def __str__(self):
        return "Axes(%g,%g;%gx%g)" % tuple(self._position.bounds)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, that's exactly what I was searching for! Thanks a lot!

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.