根据散点图创建标签及其点对象的字典 [英] Creating a dict of labels and their point objects from a scatter plot

查看:35
本文介绍了根据散点图创建标签及其点对象的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个交互式图形,我可以在其中选择散点图的点以进行进一步的操作,例如与另一点交换位置.选择该点后,它会变成黑色,表明已选择该点.

I am creating an interactive graph where I can select the points of a scatter plot for further operations e.g. swapping positions with another point. When the point is selected, it turns black to indicate that the point has been selected.

图中可能有几个散点图,每个散点图都有一个独特的颜色,所以为了保留每个点的原始颜色,我需要创建一个标签(键)及其点对象(值)的字典.配对中的其他值包括原始颜色、xy 位置等.

There may be several scatter plots in the figure and each plot has a unique color so to retain the original color for each point, I need to create a dictionary of labels (key) and their point objects (value). Other values in the pairing include original color, xy position, etc.

这是一个散点图,它是pick event函数:

Here's a single scatter plot and it's pick event function:

self.scatter = self.mplwidget_layout.canvas.ax.scatter(
    y=...,
    x=...,
    color=...,
    edgecolors=...,
    picker=True
)
self.mplwidget_layout.canvas.mpl_connect('pick_event', self.select_point)

def select_point(self, event):
    if event.mouseevent.button == 1:
        facecolor = self.scatter._facecolors[event.ind,:]

        if (facecolor == np.array([[0, 0, 0, 1]])).all():
            # Look up label-object dict 
        else:
            self.scatter._facecolors[event.ind,:] = (0, 0, 0, 1)
            self.scatter._edgecolors[event.ind,:] = (1, 1, 1, 1)

        self.mplwidget_layout.canvas.draw()

  1. 似乎我无法将单个标签(在数组中)分配给它们的点,但是我可以将偏移量映射到标签.有没有一种方法可以给每个点命名并按名称检索它们?

  1. It seems like I can't assign individual labels (in an array) to their points but I can map the offsets to the labels. Is there a way to give each point a name and retrieve them by name?

如何访问散点图中的点对象列表?

How do I access the list of point objects in a scatter plot?

点及其指定的名称:

推荐答案

为了在点击点后恢复原始颜色,您可以使用 event.ind 和您拥有的颜色列表最初用于对点进行着色.我完全看不到这里需要字典.

In order to get back the original color after clicking the point you may use the event.ind and the list of colors you have used initially to colorize the points. I do not see the need for a dictionary here at all.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(6)
y = np.random.rand(6)

fig, ax = plt.subplots()
ax.set_facecolor("k")

fcolor = plt.cm.RdYlBu(x)
ecolor = ["k"]*6

scatter = ax.scatter(x,y, s=100, facecolors=fcolor,edgecolors=ecolor , picker=True)

def select_point(event):
    if event.mouseevent.button == 1:
        facecolor = scatter._facecolors[event.ind,:]

        if (facecolor == np.array([[0, 0, 0, 1]])).all():
            scatter._facecolors[event.ind,:] = fcolor[event.ind]
            scatter._edgecolors[event.ind,:] = (0, 0, 0, 1)
        else:
            scatter._facecolors[event.ind,:] = (0, 0, 0, 1)
            scatter._edgecolors[event.ind,:] = (1, 1, 1, 1)

        fig.canvas.draw_idle()

fig.canvas.mpl_connect('pick_event', select_point)

plt.show()

这篇关于根据散点图创建标签及其点对象的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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