python - Python matplotlib 使用 Tkinter 作为仪表板
问题描述
我正在阅读 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)
解决方案
您可以使用以下代码设置后端:
import matplotlib
matplotlib.use("TkAgg")
要在另一个文件中运行代码,只需导入它。并显示 gif 使用:
from PIL import Image
img = Image.open(filename)
plt.imshow(img)
推荐阅读
- javascript - 将 JavaScript 函数限制为指定的屏幕宽度
- powershell - 脚本有效,但我不知道如何扩展 PSCustomObject
- r - 为什么 ggplot 制作的箱线图与 Prsim 等其他软件制作的箱线图不同?
- embedded-linux - 驱动程序未绑定到 i2c io 扩展器
- swiftui - SwiftUI - 更新具有太多属性的 Struct 属性的最有效方法是什么?
- docker - 可以在不修改docker desktop版本的情况下降级k8s docker desktop使用的版本吗?
- r - 如果一个参与者在不同的行中有多个条目,如何可视化数据?
- python - Django如何在发布请求结果后重定向
- node.js - 如何更改 Discord 机器人昵称?
- javascript - 为什么 onChange 和 onKeyDown 事件同时触发?有没有办法防止这种情况发生?