python - 带有动画的图像上的 Python 绘图
问题描述
我想为每个新位置连续绘制点,i
同时清除以前的帧以可视化运动。我在正确的轨道上吗?我习惯于使用该setinterval
函数在 Javascript 中对此进行编码,但我是 Python 新手。
fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.imshow(img, cmap=plt.cm.gray)
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlim(0,img.shape[1])
ax.plot(np.r_[x,x[0]], np.r_[y,y[0]], c=(0.5,0.5,0.5), lw=1)
for i, snake in enumerate(snakePoints):
if i % 15 == 0:
ax.plot(np.r_[snake[0], snake[0][0]], np.r_[snake[1], snake[1][0]], c=(0.1,0,1,0.3), lw=1)
ax.plot(np.r_[snakePoints[-1][0], snakePoints[-1][0][0]], np.r_[snakePoints[-1][1], snakePoints[-1][1][0]], c=(1,0,0), lw=3)
plt.show()
解决方案
尝试在下面使用
ax.clear() # to clear the previous output
plt.pause(0.001) # to pause and show animation for new plot
并编写一个函数来调用新参数和绘图。我想你的缩进是错误的。
推荐阅读
- jquery - 动态文本区域不会根据 Jquery 中页面加载时的输入值进行扩展
- selector - Grafana 仪表板选择器卡住
- sql-server - SSRS - 适合 PDF 导出到一页
- python - Python Selenium 中的循环列表
- operating-system - 删除一个并创建新的 MBR 分区排列
- ansible - ansible如何从另一个角色加载变量,而不执行它?
- java - 尝试在 Maven 插件中执行 jar
- javascript - Javascript:改进具有嵌套 foreach 循环的代码
- java - 如何知道使用 JavaParser 读取或修改变量
- ruby-on-rails - 从参数化查询中删除引号