应用matplotlib draw()冻结窗口解决方案到特殊情况

时间:2015-07-30 13:59:26

标签: python matplotlib

是python的新手,我发现matplotlib draw()自己冻结了窗口问题,并在这个网站上找到了解决方案:

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import random
import numpy as np
import sys
import Tkinter as tk
import time

def function1(fig, ax):
  ax.cla()
  color_grade_classes = ['#80FF00','#FFFF00','#FF8000', '#FF0000']
  varsi =  random.randint(1, 100)

  for colors, rows  in zip(color_grade_classes, [3,2,1,0] ):  
    indexs = np.arange(5)
    heights = [varsi,varsi/2,varsi/3,0,0]
    ax.bar(indexs, heights, zs = rows,  zdir='y', color=colors, alpha=0.8)
  return fig

class App():
    def __init__(self):
        self.root = tk.Tk()
        self.root.wm_title("Embedding in TK")

        self.fig = plt.figure()
        self.ax = self.fig.add_subplot(111, projection='3d')
        self.ax.set_xlabel('X')
        self.ax.set_ylabel('Y')
        self.fig = function1(self.fig, self.ax)

        self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
        self.toolbar = NavigationToolbar2TkAgg( self.canvas, self.root )
        self.toolbar.update()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        self.label = tk.Label(text="")
        self.label.pack()
        self.update_clock()
        self.root.mainloop()

    def update_clock(self):
        self.fig = function1(self.fig,self.ax)
        self.canvas.show()
        self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        now = time.strftime("%H:%M:%S")
        self.label.configure(text=now)
        self.root.after(1000, self.update_clock)

app=App()

我的问题是将以下绘图代码合并到其中。它与给出的例子不完全相同。不知道如何在函数定义和类声明之间进行拆分。任何人都可以帮我吗?

t0 = time.time()

while time.time() - t0 <= 10:

    data = np.random.random((32, 32))
    plt.clf()
    im = plt.imshow(data,cmap=cm.gist_gray, interpolation='none')
    plt.ion()
    cbar = plt.colorbar(im)
    cbar.update_normal(im)
    cbar.set_clim(0, np.amax(data))
    plt.draw()

    time.sleep(0.5)


plt.show(block=True)

1 个答案:

答案 0 :(得分:0)

这似乎奏效了。 基本上__init__部分初始化绘图并绘制第一个&#34;框架&#34;。然后每1000ms调用一次函数self.update_clock,该函数调用function1()生成新数据并重绘图。

由于示例中的颜色栏,我稍微移动了一些东西,但这个想法保持不变。

import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import random
import numpy as np
import sys
import Tkinter as tk
import time


class App():
def __init__(self):
    self.root = tk.Tk()
    self.root.wm_title("Embedding in TK")

    self.fig = plt.figure()
    self.ax = self.fig.add_subplot(111)
    self.ax.set_xlabel('X')
    self.ax.set_ylabel('Y')
    data = np.random.random((32, 32))
    im = self.ax.imshow(data,cmap=cm.gist_gray, interpolation='none')
    self.cbar = self.fig.colorbar(im)
    self.cbar.update_normal(im)
    self.cbar.set_clim(0, np.amax(data))
    self.fig = self.function1(self.fig, self.ax)

    self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
    self.toolbar = NavigationToolbar2TkAgg( self.canvas, self.root )
    self.toolbar.update()
    self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

    self.label = tk.Label(text="")
    self.label.pack()
    self.update_clock()
    self.root.mainloop()

def update_clock(self):
    self.fig = self.function1(self.fig,self.ax)
    self.canvas.show()
    self.canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
    now = time.strftime("%H:%M:%S")
    self.label.configure(text=now)
    self.root.after(1000, self.update_clock)

def function1(self, fig, ax):
    ax.cla()
    data = np.random.random((32, 32))
    im = ax.imshow(data,cmap=cm.gist_gray, interpolation='none')
    self.cbar.update_normal(im)
    self.cbar.set_clim(0, np.amax(data))
    return fig

app=App()