jumeaux-numeriques/twin_plot_qt.py

199 lines
6.7 KiB
Python
Raw Normal View History

import sys
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
from PyQt5 import QtCore, QtGui, QtWidgets # GUI Qt5
import matplotlib # Grapheur Matplotlib
import matplotlib.pyplot as plts
2023-01-31 05:06:52 +01:00
matplotlib.use('Qt5Agg')
2023-01-29 17:28:54 +01:00
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
###############################################################################
2023-01-21 10:39:24 +01:00
# twin_plot_qt.py
2023-01-31 05:06:52 +01:00
# @title: Visualisation des données (pyQt5+Matplotlib)
# @project: Blender-EduTech
# @lang: fr
# @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
# @copyright: Copyright (C) 2023 Philippe Roy
# @license: GNU GPL
###############################################################################
2023-01-25 06:03:02 +01:00
plot_config={}
###############################################################################
2023-01-21 10:39:24 +01:00
# Zone de dessin
###############################################################################
class MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100):
if plot_nb(plot_config) ==1: # plot_nb() : nombre de graphiques
fig, self.plt = plts.subplots()
else:
fig, self.plt = plts.subplots(plot_nb(plot_config), 1, sharex=True) # plot_nb() : nombre de graphiques
super(MplCanvas, self).__init__(fig)
2023-01-21 10:39:24 +01:00
###############################################################################
# Graphique dynamique
###############################################################################
class DynamicPlot(QtWidgets.QMainWindow):
# 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)]
self.xdata = [0]
self.ydata = [0]
self.update_plot()
self.show()
# Timer pour le update_plot
fps = 120 # Blender est cadencé à 60 fps
self.timer = QtCore.QTimer()
# self.timer.setInterval(int(1000/fps))
self.timer.setInterval(1)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
print("Qt : Canvas ready\n")
# Lecture des données et mise à jour du graphique
def update_plot(self):
plt = self.canvas.subplot
# Données
msg=""
for line in sys.stdin:
msg = line.rstrip()
break
print("Qt : ", msg)
# Essai
new_x=self.xdata[len(self.xdata)-1]+1
self.xdata.append(new_x)
self.ydata.append(random.randint(0, 10))
# Lecture du Pipe simple
# msg=""
# print (sys.stdin)
# contents = sys.stdin.read(1)
# # contents = sys.stdin.buffer
# print ("contents :", contents)
# for line in sys.stdin:
# msg = line.rstrip()
# break
# print(msg)
# X et Y
# FIXME : temps et une valeur
# 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
# 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])]
# 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
# Remplissage des graphiques à partir du fichier CSV
fields, xdata, ydata = csv_read(sys.argv[1])
plots_static(self.canvas.plt, fields, xdata, ydata, plot_config, sys.argv[1])
2023-01-21 10:39:24 +01:00
self.show()
###############################################################################
# Application
###############################################################################
2023-01-31 05:06:52 +01:00
if __name__ == "__main__":
# 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()
app.exec_()
sys.exit(0)