Python:鼠标单击的坐标尽可能简单

Bip*_*tes 2 python matplotlib mouseevent coordinates

我想编写一个交互式Bezier曲线生成器,唯一的输入是图形上的鼠标单击坐标(使用matplotlib.pyplot)

因此,我想知道如何获取这些坐标,以及如何使用classself.functions尽可能简单地将它们堆叠在x轴和y轴的两个列表中。

谢谢 !

双拼

arm*_*ita 6

matplotlib中有一个事件,当您单击绘图时会返回鼠标坐标。检查以下食谱:

import numpy as np
import matplotlib.pyplot as plt

class LineBuilder:
    def __init__(self, line,ax,color):
        self.line = line
        self.ax = ax
        self.color = color
        self.xs = []
        self.ys = []
        self.cid = line.figure.canvas.mpl_connect('button_press_event', self)
        self.counter = 0
        self.shape_counter = 0
        self.shape = {}
        self.precision = 10

    def __call__(self, event):
        if event.inaxes!=self.line.axes: return
        if self.counter == 0:
            self.xs.append(event.xdata)
            self.ys.append(event.ydata)
        if np.abs(event.xdata-self.xs[0])<=self.precision and np.abs(event.ydata-self.ys[0])<=self.precision and self.counter != 0:
            self.xs.append(self.xs[0])
            self.ys.append(self.ys[0])
            self.ax.scatter(self.xs,self.ys,s=120,color=self.color)
            self.ax.scatter(self.xs[0],self.ys[0],s=80,color='blue')
            self.ax.plot(self.xs,self.ys,color=self.color)
            self.line.figure.canvas.draw()
            self.shape[self.shape_counter] = [self.xs,self.ys]
            self.shape_counter = self.shape_counter + 1
            self.xs = []
            self.ys = []
            self.counter = 0
        else:
            if self.counter != 0:
                self.xs.append(event.xdata)
                self.ys.append(event.ydata)
            self.ax.scatter(self.xs,self.ys,s=120,color=self.color)
            self.ax.plot(self.xs,self.ys,color=self.color)
            self.line.figure.canvas.draw()
            self.counter = self.counter + 1

def create_shape_on_image(data,cmap='jet'):
    def change_shapes(shapes):
        new_shapes = {}
        for i in range(len(shapes)):
            l = len(shapes[i][1])
            new_shapes[i] = np.zeros((l,2),dtype='int')
            for j in range(l):
                new_shapes[i][j,0] = shapes[i][0][j]
                new_shapes[i][j,1] = shapes[i][1][j]
        return new_shapes
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_title('click to include shape markers (10 pixel precision to close the shape)')
    line = ax.imshow(data) 
    ax.set_xlim(0,data[:,:,0].shape[1])
    ax.set_ylim(0,data[:,:,0].shape[0])
    linebuilder = LineBuilder(line,ax,'red')
    plt.gca().invert_yaxis()
    plt.show()
    new_shapes = change_shapes(linebuilder.shape)
    return new_shapes

img = np.zeros((100,100,3),dtype='uint')
shapes = create_shape_on_image(img)[0]
print(shapes)
Run Code Online (Sandbox Code Playgroud)

它有点扩展(您也可以检查matplotlib示例之一),但是它可以直观地查看您单击的位置(可以放置图像而不是背景的“黑色” numpy数组)。结果应该是这样的:

matplotlib图中的鼠标单击标记

最初是为了使形状封闭而设计的,但可以满足您的需求。关闭图解后,您将获得print实际坐标:

[[54 13]
 [19 39]
 [19 77]
 [58 78]
 [93 45]
 [90 11]
 [54 13]]
Run Code Online (Sandbox Code Playgroud)

如果您希望以比较适度的方式开始(一个简单的click事件以将坐标打印到控制台),请使用以下方法:

import matplotlib.pyplot as plt

def onclick(event):
    print(event.xdata, event.ydata)

fig,ax = plt.subplots()
ax.plot(range(10))
fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
Run Code Online (Sandbox Code Playgroud)

  • 最后一段是黄金。我建议将其放在您的答案的开头,以便人们早日看到它 (2认同)