pyqt_data_analysis/PlotWindows.py

42 lines
1.4 KiB
Python
Raw Permalink Normal View History

2024-06-09 22:27:55 +08:00
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QMainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import sys
from datetime import datetime
class PlotWidget(QWidget):
# def __init__(self, parent: QWidget | None = ..., flags: Qt.WindowFlags | Qt.WindowType = ...) -> None:
# super().__init__(parent, flags)
def __init__(self, scalex, scaley, xlabel, ylabel):
super().__init__()
self.scalex = scalex
self.scaley = scaley
self.xlabel = xlabel
self.ylabel = xlabel
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
# self.ax.plot(scalex=scalex, scaley=scaley)
self.ax.plot(scalex, scaley)
self.ax.set_xlabel(xlabel=xlabel)
self.ax.set_ylabel(ylabel=ylabel)
self.canvas = FigureCanvas(self.fig)
layout = QVBoxLayout()
layout.addWidget(self.canvas)
self.setLayout(layout)
self.canvas.draw()
def save(self, dir_path):
plt.figure()
plt.plot(self.scalex, self.scaley)
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
plt.savefig(f"{dir_path}/{current_time}.png")