从 Matplotlib 中的散点图中提取数据

gje*_*ess 5 python matplotlib scatter-plot

我正在编写一个接口来在 Matplotlib 中绘制散点图,我希望能够从 python 脚本访问数据。

现在,我的界面正在做:

scat = self.axes.scatter(x_data, y_data, label=label, s=size)
Run Code Online (Sandbox Code Playgroud)

使用标准,axes.plot我可以执行以下操作:

line = self.axes.plot(x_data, y_data)
data = line[0].get_data()
Run Code Online (Sandbox Code Playgroud)

这有效。我想要的是类似的东西,但有散点图。

有人可以建议类似的方法吗?

mwa*_*kom 7

使用scatter绘制绘图PathCollection,因此 x、y 位置称为“偏移量”:

import numpy as np
import matplotlib.pyplot as plt

f, ax = plt.subplots()
scat = ax.scatter(np.random.randn(10), np.random.randn(10))

print scat.get_offsets()

[[-0.17477838 -0.47777312]
 [-0.97296068 -0.98685982]
 [-0.18880346  1.16780445]
 [-1.65280361  0.2182109 ]
 [ 0.92655599 -1.40315507]
 [-0.10468029  0.82269317]
 [-0.09516654 -0.80651275]
 [ 0.01400393 -1.1474178 ]
 [ 1.6800925   0.16243422]
 [-1.91496598 -2.12578586]]
Run Code Online (Sandbox Code Playgroud)

  • 对我来说(matplotlib==3.4.2) get_offsets 返回了一个掩码数组,所以我需要执行以下操作: scat.get_offsets().data 以获得与上面相同的结果。 (2认同)