用lasso或lassomanager选择python imshow()中的点

时间:2016-03-21 17:33:58

标签: python python-2.7 matplotlib imshow

任何人都可以帮我选择2D阵列色块中的点我用matplotlib中的imshow()进行绘图。 我发现了两个使用matplotlib中的套索或LassoSelector的例子: http://matplotlib.org/examples/widgets/lasso_selector_demo.html http://matplotlib.org/examples/event_handling/lasso_demo.html

不幸的是,我不能用imshow和2d(通常为50x500)阵列让它们为我工作。这些示例工作正常,但是当使用imshow时,python会抱怨诸如无法设置颜色之类的事情。 我仍然是python的新手。根据我的理解,我必须从我发送给imshow的数据中获取一个集合? 下面是一些不起作用的示例代码 - 我尝试使用上面链接的两个示例,但没有成功。 如果有人可以指出我正确的方向,那将是很好的,虽然我不介意工作代码:) 谢谢。

$("#searchtext").html() = ("#searchtext").html();

这里是上面链接的示例中的代码:

import numpy as np
import matplotlib.pyplot as plt
# use random sample data:
random_data = np.random.rand(5, 5)
fig, ax = plt.subplots()
# Need the plot data as collection to be used with the lasso?
pts = ax.add_collection(imshow(data, aspect='auto', origin='lower',picker=True),autolim=False)
data = [Datum(*xy) for xy in random_data]
lman = ps.LassoManager(ax, data)

编辑:以下示例适用于从2d图(不是散点图或xy图)中选择任意形状的区域。不漂亮也不优雅,但可以等待:

from matplotlib.widgets import Lasso
from matplotlib.colors import colorConverter
from matplotlib.collections import RegularPolyCollection
from matplotlib import path

import matplotlib.pyplot as plt
from numpy import nonzero
from numpy.random import rand


class Datum(object):
    colorin = colorConverter.to_rgba('red')
    colorout = colorConverter.to_rgba('blue')

    def __init__(self, x, y, include=False):
        self.x = x
        self.y = y
        if include:
            self.color = self.colorin
        else:
            self.color = self.colorout


class LassoManager(object):
    def __init__(self, ax, data):
        self.axes = ax
        self.canvas = ax.figure.canvas
        self.data = data

        self.Nxy = len(data)

#        facecolors = [d.color for d in data]
        self.xys = [(d.x, d.y) for d in data]
        self.ind = []
        fig = ax.figure
        self.collection = RegularPolyCollection(
            fig.dpi, 6, sizes=(100,),
            facecolors=facecolors,
            offsets=self.xys,
            transOffset=ax.transData)

        ax.add_collection(self.collection)

        self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)

    def callback(self, verts):
        facecolors = self.collection.get_facecolors()
        p = path.Path(verts)
        ind = p.contains_points(self.xys)
        self.ind = nonzero([p.contains_point(xy) for xy in self.xys])[0]
        for i in range(len(self.xys)):
            if ind[i]:
                facecolors[i] = colorConverter.to_rgba('red')
#                print ind
            else:
                facecolors[i] = colorConverter.to_rgba('blue')

        self.canvas.draw_idle()
        self.canvas.widgetlock.release(self.lasso)
        del self.lasso

    def onpress(self, event):
        if self.canvas.widgetlock.locked():
            return
        if event.inaxes is None:
            return
        self.lasso = Lasso(event.inaxes,
                           (event.xdata, event.ydata),
                           self.callback)
        # acquire a lock on the widget drawing
        self.canvas.widgetlock(self.lasso)

if __name__ == '__main__':
    print 'test'

    data = [Datum(*xy) for xy in rand(5, 5)]

    ax = plt.axes(xlim=(0, 1), ylim=(0, 1), autoscale_on=False)
    lman = LassoManager(ax, data)

    plt.show()

    for i in lman.ind:
        print lman.xys[i]

    print len(lman.xys)

1 个答案:

答案 0 :(得分:0)

您的代码中尚未定义任何内容。但无论如何,这是一个使用你所提供的功能的例子:

[Command]
void CmdSyncOwner(string own)
{
    //List<GameObject> players = new List<GameObject>();
    foreach (GameObject obj in GameObject.FindObjectsOfType<GameObject>())
    {
        players.Remove(obj);
        players.Add(obj);
    }

    for (int i = 0; i < players.Count; ++i)
    {
        if (players[i].tag.Contains("Ply"))
        {
            players[i].GetComponent<FPSPlayerFunction>().baseOwner = own;
        }
    }
}

,结果如下:

enter image description here