【发布时间】:2020-06-23 06:52:22
【问题描述】:
我有一个方法,它使用 matplotlib.pyplot.ginput() 从坐标区收集单击的位置,然后将图像放置在同一图中现有的坐标轴阵列中的该位置。
视觉只在第二次调用 ginput() 后更新,这意味着它的响应与用户的点击相比是滞后的,只有当他们选择下一个位置时,他们刚刚放置的图像才会显示。这是有问题的,因为放置的随机图片可能会影响他们接下来要点击的位置。
调用方法如下所示,虽然这里是绘制轴而不是添加图像:
%matplotlib
from matplotlib import pyplot
import numpy
import random
dimensions = [10,10]
visual_width = 1366
visual_height = 768
print("Established screen dimensions as "+str(visual_width)+","+str(visual_height))
fig, axarr = pyplot.subplots(dimensions[1],dimensions[0]
, num="Live game visualised"
, figsize=(visual_width, visual_height)
, clear=True)
fig.set_size_inches(visual_width, visual_height, forward=False)
overax = fig.add_subplot(111)
overax.axis("off")
#remove axes for initial rendering
for horizontal in range(0, dimensions[0]):
for vertical in range(0, dimensions[1]):
axarr[vertical, horizontal].axis('off')
text = fig.text(0,0, "", va="bottom", ha="left")
# test repeated interactions with figure, through successively adding tiles to the play area
playing = True
while playing:
click = pyplot.ginput()
if not click:
break
horizontal = int(click[0][0]*dimensions[0])
vertical = dimensions[1] - int(click[0][1]*dimensions[1])
print(str(horizontal)+","+str(vertical))
text.set_text(str(horizontal)+","+str(vertical))
axarr[vertical-1, horizontal].axis("on")
我的 python 3 脚本在 jupyter notebook 中,但我使用默认的 %matplotlib 后端弹出视觉效果。我在 Ubuntu 上使用 Chrome。
【问题讨论】:
-
显示运行的东西。编写与您的项目无关的最少代码,以重现相同的错误。在你完成之前,你怎么能说你知道问题是什么,更不用说让别人帮你解决了?
-
在上面,但同时希望有人能认出 ginput 行为...
-
我不相信它是 ginput。这就是为什么我认为您应该首先正确隔离问题
-
感谢@MadPhysicist 的推动。我现在已将其清理为运行并重现行为的上述脚本。
标签: python python-3.x matplotlib ginput