从具有多个数据集的散点图中获取x,y? [英] getting x,y from a scatter plot with multiple datasets?

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

问题描述

我有一个散点图,由对 scatter 的不同调用组成:

I have a scatter plot that is composed of different calls for scatter:

import matplotlib.pyplot as plt
import numpy as np

def onpick3(event):
    index = event.ind
    print '--------------'
    print index
    artist = event.artist
    print artist

fig_handle = plt.figure()

x,y = np.random.rand(10),np.random.rand(10)
x1,y1 = np.random.rand(10),np.random.rand(10)

axes_size = 0.1,0.1,0.9,0.9
ax = fig_handle.add_axes(axes_size)

p = ax.scatter (x,y, marker='*', s=60, color='r', picker=True, lw=2)
p1 = ax.scatter (x1,y1, marker='*', s=60, color='b', picker=True, lw=2)

fig_handle.canvas.mpl_connect('pick_event', onpick3)
plt.show()

我希望这些点是可单击的,并获取所选索引的x,y.但是,由于 scatter 被多次调用,所以两次获得相同的索引,所以我不能在 onpick3 方法内使用 x [index]

I'd like the points to be clickable, and get the x,y of the selected indexes. However since scatter is being called more than once, I get the same indexes twice, so I cant use x[index] inside the onpick3 method

有没有直接的方法来获得积分?

Is there a straightforward way to get the points?

似乎 event.artist 返回的 PathCollection 与从 scatter (pp1 在这种情况下).但是我找不到任何使用它来提取所选索引的 x,y 的方法尝试使用 event.artist.get_paths() - 但它似乎没有返回所有的散点,但只有我点击的那个..所以我真的不确定是什么event.artist 正在回馈,什么是 event.artist.get_paths() 函数正在回馈

It seems that event.artist gives back the same PathCollection that is given back from scatter (p and p1 in this case). But I couldn't find any way to use it to extract the x,y of the selected indexes Tried using event.artist.get_paths() - but it doesn't seem to be giving back all the scatter points, but only the one that I clicked on..so I'm really not sure what event.artist is giving back and what are the event.artist.get_paths() function is giving back

似乎 event.artist._offsets 给出了具有相关偏移量的数组,但是由于某些原因,当尝试使用 event.artist.offsets 我得到

it seems that event.artist._offsets gives an array with the relevant offsets, but for some reason when trying to use event.artist.offsetsI get

AttributeError: 'PathCollection' object has no attribute 'offsets'

(尽管我了解文档,它应该在那里)

(although if I understand the docs, it should be there)

推荐答案

要获取 scatter 返回的集合的 x、y 坐标,请使用 event.artist.get_offsets() (Matplotlib 具有显式的 getter 和 setter,主要是由于历史原因.get_offsets 所做的只是返回 self._offsets,但公共接口是通过getter".).

To get the x, y coordinates for the collection that scatter returns, use event.artist.get_offsets() (Matplotlib has explicit getters and setters for mostly historical reasons. All get_offsets does is return self._offsets, but the public interface is through the "getter".).

因此,请完成您的示例:

So, to complete your example:

import matplotlib.pyplot as plt
import numpy as np

def onpick3(event):
    index = event.ind
    xy = event.artist.get_offsets()
    print '--------------'
    print xy[index]


fig, ax = plt.subplots()

x, y = np.random.random((2, 10))
x1, y1 = np.random.random((2, 10))

p = ax.scatter(x, y, marker='*', s=60, color='r', picker=True)
p1 = ax.scatter(x1, y1, marker='*', s=60, color='b', picker=True)

fig.canvas.mpl_connect('pick_event', onpick3)
plt.show()

但是,如果您不按第3或第4变量来改变事物,则可能不希望使用 scatter 来绘制点.请改用 plot .scatter 返回的集合比 plot 返回的 Line2D 更难处理.(如果您确实使用了 plot ,那么您将使用 x,y = artist.get_data().)

However, if you're not varying things by a 3rd or 4th variable, you may not want to use scatter to plot points. Use plot instead. scatter returns a collection that's much more difficult to work with than the Line2D that plot returns. (If you do go the route of using plot, you'd use x, y = artist.get_data().)

最后,不要过多地插入我自己的项目,但如果你可能会发现mpldatacursor 有用.它抽象化了您在这里所做的很多事情.

Finally, not to plug my own project too much, but if you might find mpldatacursor useful. It abstracts away a lot of you're doing here.

如果您决定采用这种方式,您的代码将类似于:

If you decide to go that route, your code would look similar to:

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

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