【问题标题】:Python matplotlib using Tkinter as dashboardPython matplotlib 使用 Tkinter 作为仪表板
【发布时间】:2021-08-07 11:13:19
【问题描述】:

我正在阅读 rasberry 奶昔,以从 4 个通道读取套接字的数据并将数据写入 4 个单独的平面文件。然后我需要读取各个平面文件并使用 matplotlib 绘制数据,如下所示。但我需要使用 tkinter 来控制它。我还需要调用一个名为 DropCoverHold.py 的外部 python 代码,或者在另一个帧上显示一个 gif 文件,该帧将在更高的阈值上触发。 有没有办法用 tkinter 显示下面的 matplotlib 动画代码?

%matplotlib notebook
import datetime
import time
import os
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import TextBox
from IPython.core.display import display, HTML

#>conda update conda
#conda update ipython

display(HTML("<style>.container {width:80% !important;} </style>"))

xsEHZ = []
ysEHZ = []
xsENE = []
ysENE = []
xsENZ = []
ysENZ = []
xsENN = []
ysENN = []

inputFilePrefix = 'c:/temp/inputdata'
inputFileSuffix = '.txt'
valid_channels = ["EHZ", "ENE", "ENZ", "ENN"]

#Clear input files
for c in valid_channels:
    inputFileToClear = inputFilePrefix + c + inputFileSuffix
    o = open(inputFileToClear, "w").close()
    
fig, ((ax1, ax2, ax3, ax4)) = plt.subplots(4, 1, figsize=(8, 8))

def GenerateData(inputFile):
    data = open(inputFile, 'r').read()
    lines = data.split('\n')
    
    xs = []
    ys = []    
                
    for line in lines[-10:]:
        if len(line) > 1:
            x, y = line.split(',') # Delimiter is comma  
           
            time_formatted = time.strftime('%H:%M:%S', time.localtime(float(x)))
            stringTime = "{:.3f}".format(float(x))
            
            xs.append(time_formatted + '.' + stringTime[-3:])
            ys.append(round(float(y)))
        if os.path.exists('/temp/Threshold.txt'):
            %run "/temp/DropCoverHold.py"
            
    return xs, ys

def animate(i):
    dataInputFile = 'c:/temp/inputdataEHZ.txt'
    xsEHZ, ysEHZ = GenerateData(dataInputFile)
    
    dataInputFile = 'c:/temp/inputdataENE.txt'
    xsENE, ysENE = GenerateData(dataInputFile)
    
    dataInputFile = 'c:/temp/inputdataENZ.txt'
    xsENZ, ysENZ = GenerateData(dataInputFile)
    
    dataInputFile = 'c:/temp/inputdataENN.txt'
    xsENN, ysENN = GenerateData(dataInputFile)

    ax1.cla()
    ax2.cla()
    ax3.cla()
    ax4.cla()
    
    ax1.plot(xsEHZ, ysEHZ, scaley=True, scalex=True, color="red")
    ax1.set_xlabel('Time')
    ax1.set_ylabel('Count')
    ax1.set_title('EHZ')
    plt.setp(ax1.get_xticklabels(), rotation=30);

    ax2.plot(xsENE, ysENE, scaley=True, scalex=True, color="green")
    ax2.set_xlabel('Time')
    ax2.set_ylabel('Count')
    ax2.set_title('ENE')
    plt.setp(ax2.get_xticklabels(), rotation=30);
    
    ax3.plot(xsENZ, ysENZ, scaley=True, scalex=True, color="blue")
    ax3.set_xlabel('Time')
    ax3.set_ylabel('Count')
    ax3.set_title('ENZ')
    plt.setp(ax3.get_xticklabels(), rotation=30);
    
    ax4.plot(xsENN, ysENN, scaley=True, scalex=True, color="black")
    ax4.set_xlabel('Time')
    ax4.set_ylabel('Count')
    ax4.set_title('ENN')
    plt.setp(ax4.get_xticklabels(), rotation=30);
    
    plt.tight_layout()
    plt.show(block=False)

ani = FuncAnimation (fig, animate, interval=500, blit=True, repeat=True)

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

    您可以使用以下代码设置后端:

    import matplotlib
    matplotlib.use("TkAgg")
    

    要在另一个文件中运行代码,只需将其导入即可。 并显示 gif 使用:

    from PIL import Image
    img = Image.open(filename)
    plt.imshow(img)
    

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2018-07-11
      • 2019-12-31
      • 2020-04-09
      相关资源
      最近更新 更多