- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个交互式贝塞尔曲线生成器,唯一的输入是图形上的鼠标点击坐标(使用 matplotlib.pyplot)
因此,我想知道如何获取这些坐标以及如何使用 class
将它们堆叠在 x 轴和 y 轴的两个列表中。和 self.functions
尽可能简单。
谢谢!
双模式
最佳答案
有一个 event在 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)
它有点广泛(您也可以查看 matplotlib 示例之一)但它允许您直观地看到您单击的位置(您可以放置图像而不是背景的“黑色”numpy 数组).结果应该是这样的:
最初它是为封闭的形状而制作的,但可以根据您的需要进行调整。关闭绘图后,您将获得实际坐标的 print
:
[[54 13]
[19 39]
[19 77]
[58 78]
[93 45]
[90 11]
[54 13]]
如果您更喜欢从一些更普通的东西开始(一个简单的点击事件将坐标打印到控制台),请使用这个秘诀:
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()
关于 python : Mouse click coordinates as simply as possible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37363755/
将文件路径额外添加到图像捕获 Intent 会导致相机应用程序在 TF300t Android 平板电脑上出现故障,系统版本为 4.2.1。按“完成”按钮什么都不做——甚至不关闭相机应用程序 Acti
我开发了一个 simply.js 应用程序,它从网络服务中获取公交车到达时间,问题是截至目前它只适用于一站。 我想创建一个带有多选的配置页面,我可以在其中选择多个站点,将它们作为一个数组发送到鹅卵石,
什么是清理用户输入的字符串的简短方法。这是我在清理困惑时所依赖的代码。如果可以使用更短更智能的版本,那就太好了。 invalid = ['#','@','$','$','%','^','&','*',
我已经检查了关于这个的各种问题。第一个提供a huge question and answer (相关?不确定),第二个提供 wrong answer as best answer. 我有一个名为 g
这个问题在这里已经有了答案: The mysterious nature of Fortran 90 modules (1 个回答) 1年前关闭。 我在名为“Simply Fortran”的系统中编写
我希望 Java 不像我的努力让我相信的那样不优雅。 我正在使用 JRadioButtons,并且希望以编程方式确定与它们关联的“ID”。为此,我尝试读取 NetBeans 属性中列出的“名称”。 在
我想通读这本书:http://www.eecs.berkeley.edu/~bh/ss-toc2.html .但是我很难让“简单方案”语言工作。代码不会运行。 #lang planet dyo
我有两个列表 ItemsList 、 ilist 。如果 ilist 的节点包含与 ItemsList 的节点相同的值,我必须从 ItemsList 中删除它们,但是每次我使用下面代码中的删除函数时,
我想编写一个交互式贝塞尔曲线生成器,唯一的输入是图形上的鼠标点击坐标(使用 matplotlib.pyplot) 因此,我想知道如何获取这些坐标以及如何使用 class 将它们堆叠在 x 轴和 y 轴
我读了很多指南,也许我只是不明白。 我有一个字符串并将其转换为数组。现在我需要根据最后一列按降序排列其内容。这是我的数组在“字符串 View ”中的样子: @array = [ Machi
我正在结合伯克利 2011 年夏季 CS3 类(class)学习 Simply Scheme。我正在努力理解 subset/subsequence 过程。看到解决方案代码后,我了解了基 native
标题说明了一切。由于 C 没有异常,我不确定如何处理错误。我想到了两者的优缺点: 中止: 基本上我的意思是返回一个错误代码(将在 .h 文件中声明,可能带有自己的 perror() 类似函数)并中止该
我怀疑我的项目设置中发生了一些奇怪的事情。当我尝试仅使用方法 verifyThat() 时,编译器找不到它,并且我的程序中出现错误,但是当我写出整个内容时,org.loadui. testfx.Ass
我真的有一个分为两部分的问题。 控制台告诉我:“TypeError:this.collection.each 不是函数” 在短期内,我很想知道为什么我的代码无法正常工作。 从长远来看,我更想知道为什么
为了解决这个问题,是的,SO 上有一个类似的问题,但它并没有准确解释我认为我的情况要求什么。 在 Firebase 实时数据库中,我有一个看起来像这样的 json 树: users{ useri
我有一些长时间运行的进程,我使用 nohup 运行并将任何输出重定向到日志。由于我平时喜欢直接监听,至少在开始的时候,我也加了个尾部。这最终看起来像这样。 nohup myprocess.sh >>
以下是我遇到的问题的简化版本: 主.mk: export lst := tmp := a lst += $(tmp) $(warning lst is $(lst)) tmp := b lst +=
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在 VBA 中有一个语句来创建一个范围,然后选择并复制它。之后,我将其粘贴到其他地方。但是最后一行粘贴时没有换行,所以我想将我的范围扩大一个。我目前的说法是: Set twoRange = Rang
在 Delphi/Free Pascal 中: ^ 是一个运算符还是仅仅表示一个指针类型? 示例代码 program Project1; {$APPTYPE CONSOLE} var P: ^
我是一名优秀的程序员,十分优秀!