将三个文件夹的数据分别读取出来,然后使用绘图库来绘制三条直线,再通过左右键控制显示。如果文件太多,可以使用分页或滚动条来控制每次显示的数量。
时间: 2024-05-16 09:13:16 浏览: 72
好的,这里我使用Python的matplotlib库来绘制直线,并结合tkinter库实现左右键控制显示。
以下是代码实现:
```python
import os
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# 定义窗口大小和每页显示数量
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
PAGE_SIZE = 10
# 读取数据
data_folder1 = './data_folder1'
data_folder2 = './data_folder2'
data_folder3 = './data_folder3'
data1 = []
for filename in os.listdir(data_folder1):
with open(os.path.join(data_folder1, filename), 'r') as f:
data1.append(int(f.read().strip()))
data2 = []
for filename in os.listdir(data_folder2):
with open(os.path.join(data_folder2, filename), 'r') as f:
data2.append(int(f.read().strip()))
data3 = []
for filename in os.listdir(data_folder3):
with open(os.path.join(data_folder3, filename), 'r') as f:
data3.append(int(f.read().strip()))
# 定义绘图函数
def plot_lines(start_idx):
end_idx = start_idx + PAGE_SIZE
fig, ax = plt.subplots()
ax.plot(range(start_idx, end_idx), data1[start_idx:end_idx], label='data1')
ax.plot(range(start_idx, end_idx), data2[start_idx:end_idx], label='data2')
ax.plot(range(start_idx, end_idx), data3[start_idx:end_idx], label='data3')
ax.set_xlabel('Index')
ax.set_ylabel('Value')
ax.legend()
# 显示图像
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=1)
# 定义左右键控制函数
def on_key_press(event):
global start_idx
if event.key == 'left':
if start_idx > 0:
start_idx -= PAGE_SIZE
plot_lines(start_idx)
elif event.key == 'right':
if start_idx + PAGE_SIZE < len(data1):
start_idx += PAGE_SIZE
plot_lines(start_idx)
# 创建窗口和控件
window = tk.Tk()
window.title('Line Plot Viewer')
window.geometry(f'{WINDOW_WIDTH}x{WINDOW_HEIGHT}')
# 绑定左右键控制事件
window.bind('<Key>', on_key_press)
# 初始化显示
start_idx = 0
plot_lines(start_idx)
# 运行窗口
window.mainloop()
```
运行上述代码,将会弹出一个窗口,显示前10个数据的直线图。通过按左右键来控制每页显示的数据。
阅读全文