在Python中提取子图坐标



我是Python的新手,我实际上正在尝试在子图上绘制一个图形。

困难的是我需要axes属性,这是一个字符串,我可以通过简单地打印subplot(下面的示例)来获得。

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

这个字符串包含我想要做的所有信息(一个简单的轴([x, y, deltax, delay])。但不幸的是,我需要将print()的输出重定向到一个变量,我可以在(使用re())之后解析它。

有没有人知道如何做到这一点(我只需要这个字符串的输出,程序中的其他打印值一定不受影响)?

您可以直接访问该信息,而不是通过字符串,我认为这样更简洁:

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

我使用ippython解释器,因此通过查看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)

最新更新