从 Matplotlib 中的散点图中提取数据 [英] Extracting data from a scatter plot in Matplotlib

查看:136
本文介绍了从 Matplotlib 中的散点图中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an interface to do scatter plots in Matplotlib, and I'd like to be able to access the data from a python script.

现在,我的界面正在执行

Right now, my interface is doing:

scat = self.axes.scatter(x_data, y_data, label=label, s=size)

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

With a standard axes.plot I can do something like:

line = self.axes.plot(x_data, y_data)
data = line[0].get_data()

这行得通.我想要的是类似的东西,但是有散点图.

and that works. What I'd like is something similar, but with the scatter plot.

有人可以推荐类似的方法吗?

Can anyone suggest a similar method?

推荐答案

使用 PathCollection 绘制散点图图,因此x,y位置称为偏移".:

A scatter plot is drawn using PathCollection, so the x, y positions are called "offsets":

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

这篇关于从 Matplotlib 中的散点图中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆