简体   繁体   中英

Extracting subplot coordinates in Python

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM