Plot with Matplotlib in PyQT5

Hi, your code in the below link runs fine. But if I debug it in Pycharm, it has error:

TypeError: setCentralWidget(self, widget: QWidget): argument 1 has unexpected type ‘MplCanvas’

Would you help to correct the error.

Hi Leah welcome to the forum!

I suspect this is a mismatch between PyQt/PySide versions on your install. Matplotlib uses some logic to choose which version of Qt you have installed to use for the Matplotlib widgets. If you then import & build your UI using a different one you’ll see errors like this: because e.g. PyQt5 doesn’t recognise the MplCanvas build with PyQt6.

Do you have multiple Qt libraries installed? You can see the environment variable QT_API to the library you want to use & Matplotlib will respect that.

Alternatively, it may help to import PyQt5 before you import Matplotlib.

Let me know if that doesn’t work & we can debug it further.

Hi Martin,
I uninstalled the Pyside6 and the debug error is gone.
Thank you very much for your guidance.

1 Like