热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

matlab绘图图形之间暂停,使用Tkinter和pylab/matplotlib嵌入来播放、暂停、停止cabability的彩色绘图动画:无法更新图形/画布?...

我已经看了,但没有发现以前的问题足够具体,所以很抱歉,如果这是重复的。目标:GUI用pylab的pcolor绘制的不同矩阵数

我已经看了,但没有发现以前的问题足够具体,所以很抱歉,如果这是重复的。

目标:GUI用pylab的pcolor绘制的不同矩阵数据不断更新图形,这样就有了运行动画。但是用户应该能够通过Tkinter小部件按钮播放、暂停、停止动画。

在我使用set_array()、draw()和canvas.manager.after()得到matplotlib的答案之前,我已经有了可以启动动画的工作代码,但是在使用matplotlib功能时,我不知道如何停止或暂停它,所以我决定使用Tkinter straigt up,而不是matplotlib的Tcl/Tk包装器。这里是工作代码只是为了好玩,但万一有人有任何想法。但继续问真正的问题。# mouse click the "Play" button widget to play animation

# PROBLEMS:

# 1. can't pause or stop animation. once loop starts it cant be broken

# 2. set_array attribute for pcolor PolyCollection object only updates the matrix

# C of pcolor, however for my actual application, I will be using pcolor(x,y,C)

# and will have new x,y, and C per plot. Unlike line object, where set_xdata and

# set_ydata are used, I can't find an analogy to pcolor. If I were updating an

# image I could use set_data(x,y,C), but i am not importing an image. I assume

# pcolor is still my best bet unless (as in Matlab) there is an equivalent

# image(x,y,C) function?

import time as t

from pylab import *

from matplotlib.widgets import Button

ion()

def pressPlay(event):

#fig=figure()

ax = subplot(111)

subplots_adjust(bottom=0.2)

c=rand(5,5)

cplot=pcolor(c)

draw()

for i in range(5):

c=rand(5,5)

cplot.set_array(c.ravel())

cplot.autoscale()

title('Ionosphere '+str(i+1))

t.sleep(3)

draw()

axPlay = axes([0.7, 0.05, 0.1, 0.075])

bPlay = Button(axPlay, 'Play')

bPlay.on_clicked(pressPlay)

顺便说一下:在导入pylab时,TkAgg后端会自动设置为在matplotlib中使用。。。我想。或者我自动使用TkAgg。我正在运行Linux,Python2.6.4,iPython0.10。

我操纵了从Daniweb IT讨论社区找到的代码,以便使用Tkinter和update_idletasks()函数可以播放、编辑、停止改变标签小部件的颜色。只要安装了Tkinter,就可以在python上单独运行。没有使用matplotlib或pylab。这是工作代码,也是我最后一个问题的核心。# This is meant to draw Start and Stop buttons and a label

# The Start button should start a loop in which the label

# is configured to change color by looping through a color list.

# At each pass through the loop the variable self.stop is checked:

# if True the loop terminates.

# The Stop button terminates the loop by setting the

# variable self.stop to True.

# The Start button restarts the animation from the beginning

# if Stop was hit last, or restarts the animation from where it left off

# if pause was hit last.

# The loop also terminates on the last color of the list, as if stop were hit

from Tkinter import *

colors = ['red','green','blue','orange','brown','black','white','purple','violet']

numcol=len(colors)

class SGWidget(Frame):

def __init__(self, parent=None):

Frame.__init__(self, parent)

self.top_frame = Frame(bg='green')

self.top_frame.grid()

# enter event loop until all idle callbacks have been called.

self.top_frame.update_idletasks()

self.makeToolbar()

# construct a label widget with the parent frame top_frame

self.label = Label(self.top_frame,text = 'Text',bg='orange')

self.label.grid()

# initialize (time index t)

self.t=0

def makeToolbar(self):

self.toolbar_text = ['Play','Pause','Stop']

self.toolbar_length = len(self.toolbar_text)

self.toolbar_buttons = [None] * self.toolbar_length

for toolbar_index in range(self.toolbar_length):

text = self.toolbar_text[toolbar_index]

bg = 'yellow'

button_id = Button(self.top_frame,text=text,background=bg)

button_id.grid(row=0, column=toolbar_index)

self.toolbar_buttons[toolbar_index] = button_id

def toolbar_button_handler(event, self=self, button=toolbar_index):

return self.service_toolbar(button)

# bind mouse click on start or stop to the toolbar_button_handler

button_id.bind("", toolbar_button_handler)

# call blink() if start and set stop when stop

def service_toolbar(self, toolbar_index):

if toolbar_index == 0:

self.stop = False

print self.stop

self.blink()

elif toolbar_index == 1:

self.stop = True

print self.stop

elif toolbar_index == 2:

self.stop = True

print self.stop

self.t=0

# while in start, check if stop is clicked, if not, call blink recursivly

def blink(self):

if not self.stop:

print 'looping',self.stop

self.label.configure(bg=colors[self.t])

self.t += 1

if self.t == numcol: # push stop button

self.service_toolbar(2)

self.label.update_idletasks()

self.after(500, self.blink)

if __name__ == '__main__':

SGWidget().mainloop()

然后,在matplotlib示例embedding_in_tk.html,http://matplotlib.sourceforge.net/examples/user_interfaces/embedding_in_tk.html的帮助下,我操纵前面的代码来制作连接到pcolor图形的画布的动画。但是,使用canvas.get_tk_widget()更新画布并没有完成我假设的技巧,因为在它重新绘制pcolor()之前有一个命令。所以我想我每次重新绘制时都得把画布和图形重新连接起来?但我不知道怎么做。我希望使用update_idletasks()???我甚至在正确的轨道上???

所以,在下面的代码中,当代码循环播放时,我看到的只是同一个图,而不是一个更新的图?这是我的主要问题。from pylab import *

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

from Tkinter import *

colors=[None]*10

for i in range(len(colors)):

colors[i]=rand(5,5)

#colors = ['red','green','blue','orange','brown','black','white','purple','violet']

numcol=len(colors)

class App(Frame):

def __init__(self,parent=None):

Frame.__init__(self,parent)

self.top=Frame()

self.top.grid()

self.top.update_idletasks()

self.makeWidgets()

self.makeToolbar()

def makeWidgets(self):

# figsize (w,h tuple in inches) dpi (dots per inch)

#f = Figure(figsize=(5,4), dpi=100)

self.f = Figure()

self.a = self.f.add_subplot(111)

self.a.pcolor(rand(5,5))

# a tk.DrawingArea

self.canvas = FigureCanvasTkAgg(self.f, master=self.top)

self.canvas.get_tk_widget().grid(row=3,column=0,columnspan=3)

self.bClose = Button(self.top, text='Close',command=self.top.destroy)

self.bClose.grid()

#self.label = Label(self.top, text = 'Text',bg='orange')

#self.label.grid()

# initialize (time index t)

self.t=0

def makeToolbar(self):

self.toolbar_text = ['Play','Pause','Stop']

self.toolbar_length = len(self.toolbar_text)

self.toolbar_buttons = [None] * self.toolbar_length

for toolbar_index in range(self.toolbar_length):

text = self.toolbar_text[toolbar_index]

bg = 'yellow'

button_id = Button(self.top,text=text,background=bg)

button_id.grid(row=0, column=toolbar_index)

self.toolbar_buttons[toolbar_index] = button_id

def toolbar_button_handler(event, self=self, button=toolbar_index):

return self.service_toolbar(button)

button_id.bind("", toolbar_button_handler)

# call blink() if start and set stop when stop

def service_toolbar(self, toolbar_index):

if toolbar_index == 0:

self.stop = False

print self.stop

self.blink()

elif toolbar_index == 1:

self.stop = True

print self.stop

elif toolbar_index == 2:

self.stop = True

print self.stop

self.t=0

# while in start, check if stop is clicked, if not, call blink recursivly

def blink(self):

if not self.stop:

print 'looping',self.stop

self.a.pcolor(colors[self.t])

#draw()

#self.label.configure(bg=colors[self.t])

self.t += 1

if self.t == numcol: # push stop button

self.service_toolbar(2)

self.canvas.get_tk_widget().update_idletasks()

#self.label.update_idletasks()

self.after(500, self.blink)

#root = Tk()

app=App()

app.mainloop()

谢谢你的帮助!



推荐阅读
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • 解决python matplotlib画水平直线的问题
    本文介绍了在使用python的matplotlib库画水平直线时可能遇到的问题,并提供了解决方法。通过导入numpy和matplotlib.pyplot模块,设置绘图对象的宽度和高度,以及使用plot函数绘制水平直线,可以解决该问题。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • EzPP 0.2发布,新增YAML布局渲染功能
    EzPP发布了0.2.1版本,新增了YAML布局渲染功能,可以将YAML文件渲染为图片,并且可以复用YAML作为模版,通过传递不同参数生成不同的图片。这个功能可以用于绘制Logo、封面或其他图片,让用户不需要安装或卸载Photoshop。文章还提供了一个入门例子,介绍了使用ezpp的基本渲染方法,以及如何使用canvas、text类元素、自定义字体等。 ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
author-avatar
Li-zHihuAn
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有