使用Tkinter作为仪表板的Python matplotlib

2024-04-30 05:04:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在读取rasberry shake,从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)

Tags: importtxttruetimematplotlibplttempset
1条回答
网友
1楼 · 发布于 2024-04-30 05:04:11

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

import matplotlib
matplotlib.use("TkAgg")

要在另一个文件中运行代码,只需导入它。 要显示gif,请使用:

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

相关问题 更多 >