Ajout du grapheur plotly (pas fonctionnel)

This commit is contained in:
Philippe Roy 2023-01-14 14:03:15 +01:00
parent ce0cdc735f
commit e011bdc37f
6 changed files with 150 additions and 39 deletions

View File

@ -53,7 +53,7 @@ def commandes():
# Ecrire votre code ici ...
gyr(True) # Activer le gyrophare
test()
plot()
while True:
pass

View File

@ -1,7 +1,9 @@
import bge # Bibliothèque Blender Game Engine (UPBGE)
from twin_threading import thread_cmd_start, thread_cmd_stop, thread_cmd_end # Multithreading
import subprocess # Multiprocessus
from twin_threading import thread_cmd_start, thread_cmd_stop, thread_cmd_end # Multithreading (multitâches)
from twin_serial import jumeau, jumeau_stop, serial_close # Liaison série
from twin_plot import test # Visualisation données
import os
import sys
import time
###############################################################################
@ -118,3 +120,12 @@ def fin():
def quit():
end()
###############################################################################
# Visualisation des données
###############################################################################
# Grapheur
def plot():
# subprocess.run([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")]) # Process bloquant
subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "twin_plot.py")])

View File

@ -1,9 +1,11 @@
import bge # Bibliothèque Blender Game Engine (UPBGE)
# import bge # Bibliothèque Blender Game Engine (UPBGE)
# from twin_threading import thread_plot_start, thread_plot_end # Multithreading
import sys
import random
import matplotlib
matplotlib.use('Qt5Agg')
rom PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
@ -19,14 +21,14 @@ from matplotlib.figure import Figure
###############################################################################
# UPBGE scene
scene = bge.logic.getCurrentScene()
# scene = bge.logic.getCurrentScene()
# Récupérer le brochage du jumeau réel
# system=importlib.import_module(scene.objects['Doc']['system']) # Système
# pin_config = system.get_pin_config()
###############################################################################
# Fenêtre avec la Toobar
# Fenêtre dynamique
###############################################################################
class MplCanvas(FigureCanvasQTAgg):
@ -39,38 +41,46 @@ class MplCanvas(FigureCanvasQTAgg):
class MainWindow(QtWidgets.QMainWindow):
# Création du graphique
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
sc = MplCanvas(self, width=5, height=4, dpi=100)
sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
# Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second.
toolbar = NavigationToolbar(sc, self)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(toolbar)
layout.addWidget(sc)
# Create a placeholder widget to hold our toolbar and canvas.
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
self.setCentralWidget(self.canvas)
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
self.timer = QtCore.QTimer()
self.timer.setInterval(100)
self.timer.timeout.connect(self.update_plot)
self.timer.start()
# Lecture des données et mise à jour du graphique
def update_plot(self):
# Données
# donnees = ([0,1,2,3,4], [10,1,20,3,40])
print (self.xdata[len(self.xdata)-1]+1)
new_x=self.xdata[len(self.xdata)-1]+1
self.xdata.append(new_x)
self.ydata.append(random.randint(0, 10))
# 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)]
self.canvas.axes.cla() # Clear the canvas.
self.canvas.axes.plot(self.xdata, self.ydata, 'r')
# Trigger the canvas to update and redraw.
self.canvas.draw()
###############################################################################
# Visualisation
# Application
###############################################################################
##
# Test
##
def test():
# FIXME : mettre le multithreading
# app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
# app.exec_()
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()

73
twin_plotly.py Normal file
View File

@ -0,0 +1,73 @@
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
import plotly.express as px
import numpy as np
# import plotly.graph_objects as go
import random
import time
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.browser = QtWebEngineWidgets.QWebEngineView(self)
vlayout = QtWidgets.QVBoxLayout(self)
vlayout.addWidget(self.browser)
self.show_graph()
# self.resize(1000,800)
self.resize(500,400)
def show_graph(self):
# fig = go.Figure()
# xdata = [0]
# ydata = [0]
# df = px.data.tips()
# fig = px.box(df, x="day", y="total_bill", color="smoker")
# fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
# fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
# fig.add_trace(go.Scatter(x=xdata, y=ydata, name='essai',line=dict(color='firebrick', width=4)))
# for i in range (100):
# xdata.append(i)
# ydata.append(random.randint(0, 10))
# # fig.update_traces()
# # self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
# # fig.add_trace(go.Scatter(x=xdata, y=ydata, name='essai',line=dict(color='firebrick', width=4)))
# ydata.append(random.randint(0, 10))
# fig.update_traces()
# self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
# initialize and display plot
# fig = go.FigureWidget()
# fig.add_scatter(y=np.random.randint(0,10, 5), fill='tozeroy')
# display(fig)
xdata=[0]
ydata=[0]
fig = px.scatter(x=xdata, y=ydata)
# fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
# print (fig.data[0].x)
# print (fig.data[0].y)
# fig.show()
self.browser.setHtml(fig.to_html(include_plotlyjs='cdn'))
# modify plot using new data
for i in range(10):
time.sleep(0.5)
xdata.append(i+1)
ydata.append(random.randint(0, 10))
fig.update_traces()
# np.append(fig.data[0].x, i+2)
# np.append(fig.data[0].y, np.random.randint(0,10, 5))
# fig.update_traces()
# fig.data[0].y.append(np.random.randint(0,10, 5))
# fig.data[0].y = np.random.randint(0,10, 5)
# fig.data[0].y = np.random.randint(0,10, 5)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = Widget()
widget.show()
app.exec()

View File

@ -15,11 +15,14 @@ import time
###############################################################################
scene = bge.logic.getCurrentScene()
# Threads
threads_cmd=[]
debug_thread = scene.objects['System']['debug_thread']
# Threads de commandes
threads_cmd=[]
# # Threads de visualisation de données
# threads_plot=[]
###############################################################################
# Méthode kill pour les tâches (threads)
###############################################################################
@ -111,3 +114,17 @@ def thread_cmd_end():
time.sleep(0.125)
scene.objects['System']['thread_cmd']=False
time.sleep(0.125)
###############################################################################
# Fonctions visualisation de données
###############################################################################
# def thread_plot_start(fct):
# thread_start(threads_plot, "plot", fct)
# def thread_plot_stop():
# thread_stop(threads_plot, "plot")
# def thread_plot_end():
# if (debug_thread):
# print ("Thread plot is arrived.")