Add source example files
This commit is contained in:
63
source/plotting/gui/base_graph.py
Normal file
63
source/plotting/gui/base_graph.py
Normal file
@ -0,0 +1,63 @@
|
||||
import pandas as pd
|
||||
from PySide6.QtCore import QObject, Slot
|
||||
from PySide6.QtUiTools import QUiLoader
|
||||
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QPushButton
|
||||
from matplotlib.axes import Axes
|
||||
from matplotlib.backends.backend_qt import FigureCanvasQT
|
||||
from matplotlib.backends.backend_qtcairo import FigureCanvasQTCairo
|
||||
from matplotlib.figure import Figure
|
||||
|
||||
|
||||
class Main(QObject):
|
||||
"""Classe de gestion d'affichage de diagramme dans pyside."""
|
||||
area: QWidget = None
|
||||
window: QMainWindow | QWidget = None
|
||||
canvas: FigureCanvasQT = None
|
||||
refresh_button: QPushButton = None
|
||||
plot1: Axes = None
|
||||
data = pd.DataFrame({"age": [25, 45, 65], "prenom": ["Pierre", "Paul", "Jacques"]})
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.window = QUiLoader().load("base-window.ui")
|
||||
self.area = self.window.centralWidget()
|
||||
self.refresh_button = self.window.refresh_button
|
||||
# Créer un canevas compatible avec Matplotlib
|
||||
self.canvas = FigureCanvasQTCairo()
|
||||
self.canvas.figure = self.create_figures()
|
||||
self.area.layout().addWidget(self.canvas)
|
||||
self.show_bar()
|
||||
# Répondre à un événement sur le bouton
|
||||
self.refresh_button.clicked.connect(self.on_refresh_button_click)
|
||||
|
||||
@Slot()
|
||||
def on_refresh_button_click(self):
|
||||
"""Rafraîchir le graphique."""
|
||||
self.show_line()
|
||||
self.canvas.draw()
|
||||
|
||||
def create_figures(self) -> Figure:
|
||||
"""Générer une image avec un graphique."""
|
||||
figure = Figure(dpi=96)
|
||||
self.plot1 = figure.add_subplot(111) # 1 colonne, 1 ligne, position 1
|
||||
return figure
|
||||
|
||||
def show_bar(self):
|
||||
"""Mettre à jour le canevas avec un diagramme en barres."""
|
||||
self.canvas.figure.clear() # retirer tous les graphiques
|
||||
self.plot1 = self.canvas.figure.add_subplot(111) # 1 colonne, 1 ligne, position 1
|
||||
self.data.plot.bar(x="prenom", y="age", ax=self.plot1, rot=0)
|
||||
|
||||
def show_line(self):
|
||||
"""Mettre à jour le canevas avec un diagramme en lignes."""
|
||||
self.canvas.figure.clear() # retirer tous les graphiques
|
||||
self.plot1 = self.canvas.figure.add_subplot(111) # 1 colonne, 1 ligne, position 1
|
||||
self.data.plot.line(x="prenom", y="age", ax=self.plot1, color="#ffa820")
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
application = QApplication()
|
||||
main = Main()
|
||||
main.window.show()
|
||||
application.exec()
|
Reference in New Issue
Block a user