2023-01-15 16:56:46 +01:00
|
|
|
import sys
|
2023-01-31 13:51:32 +01:00
|
|
|
from twin_plot import plot_config_get_dict, plot_config_get, plot_config_get_enable, plot_nb, csv_read, plots_static # Gestion communes des graphiques (Qt, wx)
|
2023-01-25 06:03:02 +01:00
|
|
|
|
2024-01-02 15:13:14 +01:00
|
|
|
from PyQt6 import QtCore, QtGui, QtWidgets # GUI Qt6
|
2024-01-06 09:52:42 +01:00
|
|
|
|
2023-01-31 11:15:21 +01:00
|
|
|
import matplotlib # Grapheur Matplotlib
|
|
|
|
import matplotlib.pyplot as plts
|
2024-01-06 10:19:56 +01:00
|
|
|
matplotlib.use('QtAgg')
|
|
|
|
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
###############################################################################
|
2023-01-21 10:39:24 +01:00
|
|
|
# twin_plot_qt.py
|
2024-01-02 15:13:14 +01:00
|
|
|
# @title: Visualisation des données (pyQt6+Matplotlib)
|
2023-01-15 16:56:46 +01:00
|
|
|
# @project: Blender-EduTech
|
|
|
|
# @lang: fr
|
|
|
|
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
2024-01-02 15:13:14 +01:00
|
|
|
# @copyright: Copyright (C) 2023-2024 Philippe Roy
|
2023-01-15 16:56:46 +01:00
|
|
|
# @license: GNU GPL
|
|
|
|
###############################################################################
|
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
plot_config={}
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
###############################################################################
|
2023-01-21 10:39:24 +01:00
|
|
|
# Zone de dessin
|
2023-01-15 16:56:46 +01:00
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class MplCanvas(FigureCanvasQTAgg):
|
|
|
|
def __init__(self, parent=None, width=5, height=4, dpi=100):
|
2023-01-31 11:15:21 +01:00
|
|
|
if plot_nb(plot_config) ==1: # plot_nb() : nombre de graphiques
|
|
|
|
fig, self.plt = plts.subplots()
|
2023-01-28 23:06:55 +01:00
|
|
|
else:
|
2023-01-31 11:15:21 +01:00
|
|
|
fig, self.plt = plts.subplots(plot_nb(plot_config), 1, sharex=True) # plot_nb() : nombre de graphiques
|
2023-01-15 16:56:46 +01:00
|
|
|
super(MplCanvas, self).__init__(fig)
|
|
|
|
|
2023-01-21 10:39:24 +01:00
|
|
|
###############################################################################
|
|
|
|
# Graphique dynamique
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class DynamicPlot(QtWidgets.QMainWindow):
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
# Création du graphique
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(MainWindow, self).__init__(*args, **kwargs)
|
|
|
|
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
|
|
|
|
self.setCentralWidget(self.canvas)
|
2023-01-31 05:06:52 +01:00
|
|
|
n_data = 50
|
|
|
|
# self.xdata = list(range(n_data))
|
|
|
|
# self.ydata = [random.randint(0, 10) for i in range(n_data)]
|
2023-01-15 16:56:46 +01:00
|
|
|
self.xdata = [0]
|
|
|
|
self.ydata = [0]
|
|
|
|
self.update_plot()
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
# Timer pour le update_plot
|
2023-01-17 01:24:21 +01:00
|
|
|
fps = 120 # Blender est cadencé à 60 fps
|
2023-01-15 16:56:46 +01:00
|
|
|
self.timer = QtCore.QTimer()
|
2023-01-17 01:24:21 +01:00
|
|
|
# self.timer.setInterval(int(1000/fps))
|
|
|
|
self.timer.setInterval(1)
|
2023-01-15 16:56:46 +01:00
|
|
|
self.timer.timeout.connect(self.update_plot)
|
|
|
|
self.timer.start()
|
2023-01-17 01:24:21 +01:00
|
|
|
print("Qt : Canvas ready\n")
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
# Lecture des données et mise à jour du graphique
|
|
|
|
def update_plot(self):
|
|
|
|
plt = self.canvas.subplot
|
|
|
|
|
|
|
|
# Données
|
2023-01-18 06:06:15 +01:00
|
|
|
msg=""
|
|
|
|
for line in sys.stdin:
|
|
|
|
msg = line.rstrip()
|
|
|
|
break
|
|
|
|
print("Qt : ", msg)
|
2023-01-15 16:56:46 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Essai
|
2023-01-18 06:06:15 +01:00
|
|
|
new_x=self.xdata[len(self.xdata)-1]+1
|
|
|
|
self.xdata.append(new_x)
|
|
|
|
self.ydata.append(random.randint(0, 10))
|
2023-01-15 16:56:46 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
# Lecture du Pipe simple
|
2023-01-18 06:06:15 +01:00
|
|
|
# msg=""
|
2023-01-17 01:24:21 +01:00
|
|
|
# print (sys.stdin)
|
|
|
|
# contents = sys.stdin.read(1)
|
|
|
|
# # contents = sys.stdin.buffer
|
|
|
|
# print ("contents :", contents)
|
|
|
|
|
2023-01-18 06:06:15 +01:00
|
|
|
# for line in sys.stdin:
|
|
|
|
# msg = line.rstrip()
|
|
|
|
# break
|
|
|
|
# print(msg)
|
2023-01-15 16:56:46 +01:00
|
|
|
|
|
|
|
# X et Y
|
|
|
|
# FIXME : temps et une valeur
|
2023-01-17 01:24:21 +01:00
|
|
|
# msg_list=msg.split(',')
|
|
|
|
# print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
|
|
|
# # Lecture du Pipe
|
|
|
|
# # i=0
|
|
|
|
# # lines = sys.stdin.readlines()
|
|
|
|
# # print (lines)
|
|
|
|
# # print ("b")
|
|
|
|
|
|
|
|
# msg_line=""
|
|
|
|
# # msg_lines=[]
|
|
|
|
# for line in sys.stdin:
|
|
|
|
# # i+=1
|
|
|
|
# # print (i)
|
|
|
|
# msg_line = line.rstrip()
|
|
|
|
# # msg_lines.append(msg_line)
|
|
|
|
# # print("Qt msg_lines :", msg)
|
|
|
|
# # if i==2:
|
|
|
|
# # break
|
|
|
|
# break
|
|
|
|
# print("Qt :", msg_line)
|
|
|
|
# # print("Qt msg_lines :", msg_lines)
|
|
|
|
|
|
|
|
# # X et Y
|
|
|
|
# # FIXME : temps msg_list[0] et une seule valeur msg_list[1]
|
|
|
|
# msg_list=msg_line.split(',')
|
|
|
|
# # print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
2023-01-25 06:03:02 +01:00
|
|
|
|
2023-01-17 01:24:21 +01:00
|
|
|
|
|
|
|
# for line in msg_lines:
|
|
|
|
# msg_list=line.split(',')
|
|
|
|
# # print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
|
|
|
# for line in sys.stdin:
|
|
|
|
# msg_list=msg.split(',')
|
|
|
|
# print(msg_list)
|
|
|
|
# if msg_list[0] != "":
|
|
|
|
# self.xdata = self.xdata + [float(msg_list[0])]
|
|
|
|
# self.ydata = self.ydata + [float(msg_list[1])]
|
|
|
|
|
2023-01-15 16:56:46 +01:00
|
|
|
# self.ydata = self.ydata + [random.randint(0, 10)]
|
|
|
|
# Drop off the first y element, append a new one.
|
|
|
|
# self.ydata = self.ydata[1:] + [random.randint(0, 10)]
|
|
|
|
|
|
|
|
# Redraw
|
|
|
|
plt.cla()
|
|
|
|
plt.plot(self.xdata, self.ydata, 'r')
|
|
|
|
self.canvas.draw()
|
|
|
|
|
2023-01-21 10:39:24 +01:00
|
|
|
###############################################################################
|
|
|
|
# Graphique statique
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
class MainWindow(QtWidgets.QMainWindow):
|
|
|
|
|
|
|
|
# Création du graphique
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# Zone graphique (barre d'outil et graphique)
|
2023-01-25 22:38:26 +01:00
|
|
|
# FIXME : perd de la place quand on agrandi
|
2023-01-21 10:39:24 +01:00
|
|
|
super(MainWindow, self).__init__(*args, **kwargs)
|
|
|
|
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
|
2023-01-25 22:38:26 +01:00
|
|
|
# print (self.canvas.getContentsMargins())
|
2023-01-21 10:39:24 +01:00
|
|
|
toolbar = NavigationToolbar(self.canvas, self)
|
2023-01-25 22:38:26 +01:00
|
|
|
# print (toolbar.getContentsMargins())
|
2023-01-25 06:57:05 +01:00
|
|
|
|
2023-01-25 06:03:02 +01:00
|
|
|
# Implantation de la fenêtre
|
2023-01-21 10:39:24 +01:00
|
|
|
layout = QtWidgets.QVBoxLayout()
|
2023-01-25 22:38:26 +01:00
|
|
|
# print (layout.getContentsMargins())
|
|
|
|
# layout.setContentsMargins(0,0,0,0)
|
2023-01-21 10:39:24 +01:00
|
|
|
layout.addWidget(toolbar)
|
|
|
|
layout.addWidget(self.canvas)
|
|
|
|
widget = QtWidgets.QWidget()
|
2023-01-25 22:38:26 +01:00
|
|
|
# print (widget.getContentsMargins())
|
2023-01-21 10:39:24 +01:00
|
|
|
widget.setLayout(layout)
|
|
|
|
self.setCentralWidget(widget)
|
2023-01-25 22:38:26 +01:00
|
|
|
# print (self.getContentsMargins())
|
2023-01-21 10:39:24 +01:00
|
|
|
|
2023-02-03 05:58:20 +01:00
|
|
|
# Remplissage des graphiques à partir du fichier CSV
|
2023-01-31 11:15:21 +01:00
|
|
|
fields, xdata, ydata = csv_read(sys.argv[1])
|
2023-02-01 18:18:37 +01:00
|
|
|
plots_static(self.canvas.plt, fields, xdata, ydata, plot_config, sys.argv[1])
|
2023-01-21 10:39:24 +01:00
|
|
|
self.show()
|
|
|
|
|
2023-01-15 16:56:46 +01:00
|
|
|
###############################################################################
|
|
|
|
# Application
|
|
|
|
###############################################################################
|
|
|
|
|
2023-01-31 05:06:52 +01:00
|
|
|
if __name__ == "__main__":
|
2023-01-31 11:15:21 +01:00
|
|
|
|
|
|
|
# Configuration des plots
|
|
|
|
plot_config=plot_config_get_dict()
|
|
|
|
|
|
|
|
# Fenêtre
|
2023-01-31 05:06:52 +01:00
|
|
|
app = QtWidgets.QApplication(sys.argv)
|
|
|
|
# w = DynamicPlot()
|
|
|
|
w = MainWindow() # StaticPlot()
|
2024-01-06 09:52:42 +01:00
|
|
|
app.exec()
|
2023-01-31 05:06:52 +01:00
|
|
|
sys.exit(0)
|