簡體   English   中英

從Matplotlib散點圖恢復數據

[英]Recover data from matplotlib scatter plot

從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]路徑集合?

給定plt.scatter返回的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 ,您可以通過ax.collections訪問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.  ]])

您還可以獲取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)

要獲取x,y,z的信息:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM