提取有关matplotlibfigure中所有对象的路径或位置信息

时间:2016-06-02 08:42:54

标签: python serialization matplotlib

我想在matplotlib图中导出有关所有可见对象的信息,但是我找不到给我这个位置的参数。基本上我正在尝试序列化它。我已经阅读matplotlib figure serialization并且我同意序列化代码而不是信息更好,但matplotlib无法完全满足我的需求(Spot Colors),所以我想将所有信息发送到pdflib,这是能够做到这一点。

我到目前为止

import matplotlib.pyplot as plt
x = range(10)
y = [c**2 for c in x]

fig, sp = plt.subplots(1,1)

plt.plot(x,y)
plt.show()

path = sp.get_children()[0].get_path()
for vertex in path.iter_segments():
    print(vertex)

导出我的数据线路径,工作正常。但我无法抓住,刻度线或网格线的路径/线。这些职位是一个开始,但我也需要终点。

如果你提供它们所在的坐标系,那将是最有帮助的,所以我可以应用正确的转换。

编辑:

在数据坐标中找到轴作为刺。 找到网格线作为轴的一部分。

1 个答案:

答案 0 :(得分:0)

当您使用get_children时,您只获得子对象,但这些对象可能有自己的子对象。但是并非所有对象都有get_path方法:

import matplotlib.pyplot as plt

def get_info(obj):
    if 'get_children' in dir(obj):
        for o in obj.get_children():
            print o
            if('get_path' in dir(o)): print o.get_path()
            get_info(o)

x = range(10)
y = [c**2 for c in x]
fig,sp = plt.subplots(1,1)
plt.plot(x,y)

get_info(fig)

输出:

Rectangle(0,0;1x1)
Path(array([[ 0.,  0.],
       [ 1.,  0.],
       [ 1.,  1.],
       [ 0.,  1.],
       [ 0.,  0.]]), array([ 1,  2,  2,  2, 79], dtype=uint8))
Axes(0.125,0.1;0.775x0.8)
Line2D(_line0)
Path(array([[  0.,   0.],
       [  1.,   1.],
       [  2.,   4.],
       [  3.,   9.],
       [  4.,  16.],
       [  5.,  25.],
       [  6.,  36.],
       [  7.,  49.],
       [  8.,  64.],
       [  9.,  81.]]), None)
Spine
Path(array([[ 13.,   1.],
       [ 13.,   1.]]), None)
Spine
Path(array([[  1.,  13.],
       [  1.,  13.]]), None)
Spine
Path(array([[ 13.,   0.],
       [ 13.,   0.]]), None)
Spine
Path(array([[  0.,  13.],
       [  0.,  13.]]), None)
XAxis(80.000000,48.000000)
Text(0.5,0,u'')
Text(1,0,u'')
<matplotlib.axis.XTick object at 0x104447f90>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x1045e6050>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x10720ac50>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x1072230d0>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x107223810>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x107223f50>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x10722c6d0>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x10722ce10>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x107237590>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
<matplotlib.axis.XTick object at 0x107237cd0>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 0.,  1.]]), None)
Line2D((0,0),(0,1))
Path(array([[ 0.,  0.],
       [ 0.,  1.]]), None)
Text(0,0,u'')
Text(0,1,u'')
YAxis(80.000000,48.000000)
Text(0,0.5,u'')
Text(0,0.5,u'')
<matplotlib.axis.YTick object at 0x1045c29d0>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x104447f10>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x107241990>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x107246410>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x107246910>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x107251090>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x1072517d0>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x107237f50>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x10725c310>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
<matplotlib.axis.YTick object at 0x10725ca50>
Line2D()
Path(array([[ 0.,  0.]]), None)
Line2D()
Path(array([[ 1.,  0.]]), None)
Line2D((0,0),(1,0))
Path(array([[ 0.,  0.],
       [ 1.,  0.]]), None)
Text(0,0,u'')
Text(1,0,u'')
Text(0.5,1,u'')
Text(0,1,u'')
Text(1,1,u'')
Rectangle(0,0;1x1)
Path(array([[ 0.,  0.],
       [ 1.,  0.],
       [ 1.,  1.],
       [ 0.,  1.],
       [ 0.,  0.]]), array([ 1,  2,  2,  2, 79], dtype=uint8))