Question about error when using PyQt6 and Matplotlib
Goal:
I am developing a PyQt6 application that uses Matplotlib for graphing. However, I encountered an error when running the program.
Description of the error:
When I run program, I encountered in this error:
Traceback (most recent call last):
File "d:\programforpyqt6\Program\program.py", line 4, in <module>
from matplotlib.backends.backend_qt6agg import FigureCanvasQTAgg as FigureCanvas
ModuleNotFoundError: No module named 'matplotlib.backends.backend_qt6agg'
This is my Python code:
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
import sys
from matplotlib.backends.backend_qt6agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import platform
import psutil
class 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()
Additional information:
- Version of Python: 3.12.1
- Operating System: Windows 10
- PyQt6 version: 6.6.1
- Matplotlib version: 3.8.2
- PyQt6 install method:
pip install PyQt6 - Matplotlib install method:
pip install matplotlib - Virtual environment: I'm not using any virtual environment.
- I am working on: a PyQt6 application that involves using matplotlib for plotting.
Steps taken:
- Check version of PyQt6 by using this command on Command Prompt:
pip show PyQt6 - Reinstall matplotlib library by using this command on Command Prompt:
pip uninstall matplotlibandpip 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?