2013-05-17 101 views
1

我是Python的新手,我實際上試圖在一個子圖上繪製一個圖形。在Python中提取子圖座標

困難的是,我需要軸屬性,這是一個字符串,我可以通過簡單地打印子圖(下面的示例)獲得。

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

此字符串包含我需要的所有信息,我想要做的(一個簡單的軸([X,Y,DELTAX,DELTAY])。但不幸的是,我需要重定向打印的輸出( )到一個變量,我可以解析(用re())

有沒有人有一個想法如何做到這一點)?

回答

2

而不是通過一個字符串,您可以直接訪問該信息,我認爲這是更清潔:

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

雖然你可以有字符串,如果你喜歡:

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

我使用IPython的解釋,所以很容易通過在源尋找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) 
+0

完美,這正是我正在尋找的!非常感謝! – bserra