2015-10-25 2 views
1

Из графика разметки matplotlib, я пытаюсь восстановить данные о точках. РассмотримВосстановить данные из графика разметки matplotlib

from matplotlib import pyplot as plt 
import numpy as np 
fig = plt.figure() 
x = np.linspace(0.0, 1.0, 5) 
y = np.linspace(0.0, 1.0, 5) 
plt.scatter(x, y) 

ax = fig.get_children()[1] 
pc = ax.get_children()[2] 
for path in pc.get_paths(): 
    print 
    print('path:') 
    print(path) 
    print 
    print('segments:') 
    for vert, code in path.iter_segments(): 
     print(code, vert) 

plt.show() 

Это дает

path: 
Path(array([[ 0.  , -0.5  ], 
     [ 0.13260155, -0.5  ], 
     [ 0.25978994, -0.44731685], 
     [ 0.35355339, -0.35355339], 
     [ 0.44731685, -0.25978994], 
     [ 0.5  , -0.13260155], 
     [ 0.5  , 0.  ], 
     [ 0.5  , 0.13260155], 
     [ 0.44731685, 0.25978994], 
     [ 0.35355339, 0.35355339], 
     [ 0.25978994, 0.44731685], 
     [ 0.13260155, 0.5  ], 
     [ 0.  , 0.5  ], 
     [-0.13260155, 0.5  ], 
     [-0.25978994, 0.44731685], 
     [-0.35355339, 0.35355339], 
     [-0.44731685, 0.25978994], 
     [-0.5  , 0.13260155], 
     [-0.5  , 0.  ], 
     [-0.5  , -0.13260155], 
     [-0.44731685, -0.25978994], 
     [-0.35355339, -0.35355339], 
     [-0.25978994, -0.44731685], 
     [-0.13260155, -0.5  ], 
     [ 0.  , -0.5  ], 
     [ 0.  , -0.5  ]]), array([ 1, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 
     4, 4, 4, 4, 4, 4, 4, 4, 79], dtype=uint8)) 

segments: 
(1, array([ 0. , -0.5])) 
(4, array([ 0.13260155, -0.5  , 0.25978994, -0.44731685, 0.35355339, 
     -0.35355339])) 
(4, array([ 0.44731685, -0.25978994, 0.5  , -0.13260155, 0.5  , 0. 
])) 
(4, array([ 0.5  , 0.13260155, 0.44731685, 0.25978994, 0.35355339, 
     0.35355339])) 
(4, array([ 0.25978994, 0.44731685, 0.13260155, 0.5  , 0.  , 
     0.5  ])) 
(4, array([-0.13260155, 0.5  , -0.25978994, 0.44731685, -0.35355339, 
     0.35355339])) 
(4, array([-0.44731685, 0.25978994, -0.5  , 0.13260155, -0.5  , 0. 
])) 
(4, array([-0.5  , -0.13260155, -0.44731685, -0.25978994, -0.35355339, 
     -0.35355339])) 
(4, array([-0.25978994, -0.44731685, -0.13260155, -0.5  , 0.  , 
     -0.5  ])) 
(79, array([ 0. , -0.5])) 
/usr/local/lib/python2.7/dist-packages/matplotlib/collections.py:590: 
FutureWarning: elementwise comparison failed; returning scalar instead, but in 
the future will perform elementwise comparison 
    if self._edgecolors == str('face'): 

, но я не вижу какой-либо из этого сопоставления данных с фактическими входными Разброс данных. Возможно, это не коллекция путей ax.get_children()[2], на которую мне нужно посмотреть?

ответ

4

Учитывая PathCollection возвращаемый plt.scatter, вы могли бы назвать его get_offsets метод:

from matplotlib import pyplot as plt 
import numpy as np 
fig = plt.figure() 
x = np.linspace(0.0, 1.0, 5) 
y = np.linspace(0.0, 1.0, 5) 
s = plt.scatter(x, y) 

print(s.get_offsets()) 
# [[ 0. 0. ] 
# [ 0.25 0.25] 
# [ 0.5 0.5 ] 
# [ 0.75 0.75] 
# [ 1. 1. ]] 

Или, учитывая axes объект, ax, вы можете получить доступ к PathCollection через ax.collections, а затем вызвать get_offsets:

In [110]: ax = fig.get_axes()[0] 
In [129]: ax.collections[0].get_offsets() 
Out[131]: 
array([[ 0. , 0. ], 
     [ 0.25, 0.25], 
     [ 0.5 , 0.5 ], 
     [ 0.75, 0.75], 
     [ 1. , 1. ]]) 
0

Вы также можете получить координату z. В случае, если вы использовали 3d данные:

from matplotlib import pyplot as plt 
import numpy as np 
fig = plt.figure() 
x = np.linspace(0.0, 1.0, 5) 
y = np.linspace(0.0, 1.0, 5) 
z = np.linspace(0.0, 10, 5) 
s = plt.scatter(x, y, c=z) 
cbar=plt.colorbar(s) 

Для получения информации о х, у, г:

ax=fig.get_axes()[0] 
x_r=ax.collections[0].get_offsets()[:,0] 
y_r=ax.collections[0].get_offsets()[:,1] 
z_r=ax.collections[0].get_array()