簡體   English   中英

為什么我的圖沒有使用 Tkinter 與 set_data 一起出現?

[英]Why are my plots not appearing with set_data using Tkinter?

我正在嘗試改進我的繪圖功能。 我想使用來自 EEG 板的plotGraph函數實時繪制數據,從 LSL @ 250Hz 中提取樣本。 以前,我有一個使用常規self.ax.plot(x,y)的功能版本self.ax.plot(x,y)每次繪圖需要刷新時都使用self.ax.clear()清除數據。 盡管如此,一些分析表明,與其余代碼相比,我的代碼花費了太多時間來繪制。

我得到的建議之一是使用set_data而不是 plot 和 clear。 我有多行數據要同時繪制,因此我嘗試遵循Matplotlib 多條動畫多行,您可以在下面看到(改編代碼)。 另外,有人告訴我使用self.figure.canvas.draw_idle() ,我試過了,但我不確定我是否正確。

不幸的是,它沒有用,圖表沒有更新,我似乎找不到原因。 我知道我剛才提到的來源使用animation.FuncAnimation但我不確定這會是問題所在。 是嗎?

關於為什么我的畫布圖表中沒有顯示任何線條的任何想法?

import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
class AppWindow:
   def plotGraph(self, x, y):
        for lnum,line in enumerate(self.lines):
            line.set_data(x[:], y[:, lnum])
        self.figure.canvas.draw_idle()
        plt.ylabel('Magnitude', fontsize = 9, color = tx_color)
        plt.xlabel('Freq', fontsize = 9, color = tx_color)
        self.figure.canvas.draw()

   def __init__(self):
      self.root = tk.Tk() #start of application
      self.canvas = tk.Canvas(self.root, height = 420, width = 780, bg = 
   bg_color, highlightthickness=0)
      self.canvas.pack(fill = 'both', expand = True)
      self.figure = plt.figure(figsize = (5,6), dpi = 100)
      self.figure.patch.set_facecolor(sc_color)
      self.ax = self.figure.add_subplot(111)
      self.ax.clear()
      self.line, = self.ax.plot([], [], lw=1, color = tx_color)
      self.line.set_data([],[])

      #place graph
      self.chart_type = FigureCanvasTkAgg(self.figure, self.canvas)
      self.chart_type.get_tk_widget().pack()

      self.lines = []
      numchan = 8 #let's say I have 8 channels
      for index in range(numchan):
          lobj = self.ax.plot([],[], lw=2, color=tx_color)[0]
          self.lines.append(lobj)
      for line in self.lines:
      line.set_data([],[])
  
def start(self):
   self.root.mainloop()

您的圖表是空的,因為您正在繪制空數組:

line.set_data([],[])

如果您填寫線數組,圖表繪制正確。

試試這個代碼。 它每秒用新的隨機數據更新圖表。

import tkinter as tk
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import random

bg_color='grey'
tx_color='green'
sc_color='linen'

numchan = 8
chlen = 100
xvals=[(x-40)/20 for x in range(chlen)]  # X coordinates
chcolors= ['gold','blue','green','maroon','red','brown','purple','cyan']


class AppWindow:
   def plotGraph(self):
      self.figure.canvas.draw_idle()
      plt.ylabel('Magnitude', fontsize = 9, color = tx_color)
      plt.xlabel('Freq', fontsize = 9, color = tx_color)
      self.figure.canvas.draw()
        
   def UpdateChannelData(self):  # callback with new data
      # fake random data
      for i,ch in enumerate(self.chdata):
         for p in range(len(ch)):
            ch[p] += (random.random()-.5)/100
         self.lines[i].set_data(xvals, ch)
         
      self.plotGraph()
      self.root.after(100, self.UpdateChannelData)  # simulate next call

   def __init__(self):
      global chzero
      self.root = tk.Tk() #start of application
      self.canvas = tk.Canvas(self.root, height = 420, width = 780, bg = bg_color, highlightthickness=0)
      self.canvas.pack(fill = 'both', expand = True)
      self.figure = plt.figure(figsize = (5,6), dpi = 100)
      self.figure.patch.set_facecolor(sc_color)
      self.ax = self.figure.add_subplot(111)
      self.ax.clear()
      self.line, = self.ax.plot([], [], lw=1, color = tx_color)
      self.line.set_data([],[])

      #place graph
      self.chart_type = FigureCanvasTkAgg(self.figure, self.canvas)
      self.chart_type.get_tk_widget().pack()

      self.lines = []
      #numchan = 8 #let's say I have 8 channels
      for index in range(numchan):
          lobj = self.ax.plot([],[], lw=1, color=chcolors[index])[0]
          self.lines.append(lobj)
      # set flat data
      self.chdata = [[0 for x in range(chlen)] for ch in range(numchan)]
      self.root.after(1000, self.UpdateChannelData) # start data read
  
   def start(self):
       self.root.mainloop()
       
AppWindow().start()

輸出:

腦電圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM