My code Python:
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButtonimport sysfrom matplotlib.backends.backend_qt6agg import FigureCanvasQTAgg as FigureCanvasimport matplotlib.pyplot as pltfrom matplotlib.figure import Figureimport platformimport psutilclass HardwareMonitorApp(QMainWindow): def __init__(self): super().__init__() self.central_widget = QWidget(self) self.setCentralWidget(self.central_widget) self.layout = QVBoxLayout(self.central_widget) self.cpu_button = QPushButton('Show CPU Usage', self) self.cpu_button.clicked.connect(self.show_cpu_usage) self.layout.addWidget(self.cpu_button) self.memory_button = QPushButton('Show Memory Usage', self) self.memory_button.clicked.connect(self.show_memory_usage) self.layout.addWidget(self.memory_button) self.setWindowTitle('Hardware Monitor App') def show_cpu_usage(self): x = list(range(1, 11)) y = psutil.cpu_percent(interval=1, percpu=True) self.plot_graph(x, y, 'CPU Usage', 'Core', 'Usage (%)') def show_memory_usage(self): virtual_memory = psutil.virtual_memory() swap_memory = psutil.swap_memory() labels = ['Used', 'Available', 'Total'] sizes = [virtual_memory.used, virtual_memory.available, virtual_memory.total] self.plot_pie_chart(labels, sizes, 'Memory Usage') def plot_graph(self, x, y, title, xlabel, ylabel): figure = Figure(figsize=(6, 4), tight_layout=True) plot = figure.add_subplot(1, 1, 1) plot.plot(x, y, marker='o') plot.set_title(title) plot.set_xlabel(xlabel) plot.set_ylabel(ylabel) canvas = FigureCanvas(figure) self.layout.addWidget(canvas) def plot_pie_chart(self, labels, sizes, title): figure, ax = plt.subplots() ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. ax.set_title(title) canvas = FigureCanvas(figure) self.layout.addWidget(canvas)def main(): app = QApplication(sys.argv) main_win = HardwareMonitorApp() main_win.show() sys.exit(app.exec())if __name__ == '__main__': main()
My error:
Traceback (most recent call last): File "d:\programforpyqt6\Program\program.py", line 4, in <module> from matplotlib.backends.backend_qt6agg import FigureCanvasQTAgg as FigureCanvasModuleNotFoundError: No module named 'matplotlib.backends.backend_qt6agg'
I have already installed the matplotlib library (version 3.8.2) using pip, and my PyQt6 version is 6.6.1. Despite this, I'm still unable to run the program.
Additional Information:
- My Python version: 3.12.1
- Operating System: Windows 10
- PyQt6 install method:
pip install PyQt6
- matplotlib install method:
pip install matplotlib
- Virtual environment: I'm not using a virtual environment
- I am working on: a PyQt6 application that involves using matplotlib for plotting.
Steps taken:
- Checking the version of PyQt6 by use the command on Command Prompt:
pip show PyQt6
- Reinstall matplotlib by use the command on Command Prompt:
pip uninstall matplotlib
andpip install matplotlib
My question for this error:
- Are there any compatibility issues between PyQt6 (version 6.6.1) and matplotlib (version 3.8.2)?
- Is there a different import statement I should be using for PyQt6 and matplotlib compatibility?
- Are there any known issues or workarounds for this problem?