Running matplotlib with PyQt on Windows - python

I'm having trouble running matplotlib with PyQt on Windows 7.
When executing any code that uses these two libraries, even demo code such as: http://eli.thegreenplace.net/files/prog_code/qt_mpl_bars.py.txt
I get this error:
TypeError: 'PySide.QtGui.QWidget.setParent' called with wrong argument types:
PySide.QtGui.QWidget.setParent(QWidget)
Supported signatures:
PySide.QtGui.QWidget.setParent(PySide.QtGui.QWidget)
PySide.QtGui.QWidget.setParent(PySide.QtGui.QWidget, PySide.QtCore.Qt.WindowFl
ags)
I'm using Python 2.7.5, matplotlib 1.2.1, and PyQt 4.10.3. All of these are 32-bit, running on my 64-bit OS. I've ran code which used matplotlib and PyQt separately with no issues.
I've tried the solution suggested in Getting PySide to work with matplotlib with no success.
Please help me track down this error.

I am not clear if you want to use PySide or PyQt, but in either case the problem is you want to use one, but matplotlib is setting up the Qt4 backend using the other one, hence the confusing looking errors.
If you want to use PySide make sure
backend.qt4 : PySide # PyQt4 | PySide
is in your matplotlibrc file.
If you want to use PyQt make sure
backend.qt4 : PyQt4 # PyQt4 | PySide
is in your matplotlibrc file.

Related

sudden Qt error when using matplotlib on cmd

There are questions referring to this same error message, but the crucial difference is that I am not (deliberately) using Qt. I don't actually know what it is, and I am trying to run my program on cmd, not through any IDE. I have been using matplotlib without any problems until just now, when i suddenly got the error message:
This application failed to start because it could not find or load the Qt platform plugin "windows"
in "".
Available platform plugins are: direct2d, minimal, offscreen, windows.
Reinstalling the application may fix this problem.
here is a sample program which produces this error, although I suspect it is a consequence of the package being used, not the program
import matplotlib.pyplot as plt
nums = [1,2,3,4]
labels = ["one","two","three","four"]
colours, one, two = plt.pie(nums, labels=labels, autopct='%1.1f%%')
plt.legend(colours, labels, loc="best")
plt.axis('equal')
plt.show()
again, I installed matplotlib years ago, and have been using it just fine until now. Any help would be greatly appreciated.
edit: python version 3.7.1, matplotlib version 3.1.1
Ok, I just fixed it by upgrading matplotlib to version 3.2.1
not sure why that worked but hey

Qt library incompatibility with Pycharm new installation

I installed a new version of Pycharm (2019.3.1) and running my old codes on them. But I am getting error for qt library incompatibility. I tried to update the qt package from conda (as I am using conda distribution for the python).
I noticed the following behavior -
The error comes only for the plotting (which makes sense as qt is for graphical purposes).
There is no problem in code compilation and running, as it shows the result for the 1 epoch (can be seen in the below image.
My installed qt version is 5.9 (you can see in the second image)
The same code runs perfectly in spider but throwing the below error in pycharm. Please help me to fix this problem. thanks
From error message you posted it is clear that you have library incompatibility problem.
Somehow it appears you have 2 Qt versions installed - 5.9.7 and 5.12.4 and these versions have conflict with each other.
You need to verify Run configuration in PyCharm to see which python environment it uses. And then check why 2 Qt are available in your system.
Anothe option that comes to my mind - your PyQt5 or PySide2 (whatever you use) library is built with Qt 5.12.4 while your system have 5.9.7 and it brings a conflict.

Which backend for matplotlib using MacOS?

The question of using matplotlib with MacOS is a tricky one which has already been thoroughly reviewed by a number of discussions (see below). The problem is the following:
using MacOS Mojave 10.14.3
using python 3.7.2 in a conda environment
using matplotlib 3.0.3
Here is the simplest code snippet I came up with which allows reproducing the issue:
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [1, 2, 3]
plt.plot(x, y)
plt.show()
This throws the following error:
2019-03-22 12:25:43.429 python3.7[22209:554135] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0
2019-03-22 12:25:43.431 python3.7[22209:554135] \*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0'
*** First throw call stack:([...])
libc++abi.dylib: terminating with uncaught exception of type NSException
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
The issue is documented here. One solution is to install the PyQt5 package to your Python installation and to add the following lines at the beginning of your script:
import matplotlib
matplotlib.use("Qt5Agg")
While this works perfectly well, I am wondering why other backends fail to provide similar behavior.
Indeed I tried using MacOSX backend :
import matplotlib
matplotlib.use('MACOSX')
Which yields to the error:
from matplotlib.backends import _macosx
ImportError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.
The issue is documented here, there and in plenty of other threads.
Two solutions came out :
install python.app(conda install python.app) and launch your script with pythonw instead of python
use TKAggbackend
Using the first one works well but I wonder:
why do we need to call pythonw instead of python ?
what exactly is the python.app package ?
how can we make this solution work using an IDE (let say PyCharm for instance) ?
As for the second one, it does "work" up to a certain point: when running matplotlib using TkAgg, the plot window is really buggy. Indeed, it often needs several clicks on the "zoom", "pan" or "home" buttons to get them to actually work. It really is a great pain to use it. I asked several colleagues or friends using matplotlib with TkAgg and they all have the same issue.
Does anyone know the reason for this state of fact? Or if there is any workaround to avoid this issue (apart from installing pyqt5)?
Using the first option is your best bet since you are already working with a virtual environment.
According to matplotlib, there are two variants of python:
A Framework build - Quite important for GUI stuffs in MacOXs
A regular build.
Matplotlib in this case would want to interact natively with OSX and for this, it needs the Framework build this is the reason why installing the python.app type of python is important.
More information can be gotten from Matplotlib FAQ.
Check this link for more about the need for a framework build python.
I'm going to make some assumptions. If they're wrong I apologize.
You installed Python with Anaconda.
Personally, I've never had any problems on mac with matplotlib. My setup is: Mojave, Python3.7.3 in a venv using the python built in module (python3 -m venv), and matplotlib 3.0.3.
I can't answer your question on how to fix your problem, but I'm kind of trying to answer your "is there any workaround" question. Personally, I've always had issues with using Anaconda/Spyder/Conda for Python. I've always felt installing it as its own binary/app on the system leads to the fewest errors.
Now, I'm not saying you have to download and install by hand though. I use homebrew and it saves me headaches everyday I assume (such as upgrading applications and packages). That's the "work around" I'd suggest. Because isn't installing via Anaconda/Spyder already a workaround to installing Python properly? I've always felt performing one work around requires more workarounds for full functionality. Such as having to specify the matplotlib backend when it should be detected by default.
Obviously, I'm a little biased against that tool and that may be reflected in this answer, so take it with a grain of salt. Even though Conda is a legitimate tool that I think is useful, I find it annoying having to use both pip and conda when conda doesn't contain the packages I want.
There are two things you can try.
You can read Matplotlib info page on that, https://matplotlib.org/3.1.0/faq/osx_framework.html,
The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.
And follow the instructions.
Or simply follow the error message you get when you try %matplotlib inline,
(...)
UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
I did %matplotlib osx and have plt.imshow(myimg) working just fine afterwards.

matplotlib does not import PyQt4, PyQt5 or PySide

I'm using Canopy 2.1.3 distribution (on Ubuntu 16.04) which as you may know contains the matplotlib package. Simply running in the Canopy's shell import matplotlib.pyplot as plt gives me
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I have installed all of the three packages with Synaptic but the problem persists.
Could this be related to environment variables? Or, could it be because Canopy's Python 3.5 and Ubuntu's Python 2.7 are conflicting?
EDIT:
I have finally used the PyQt by installing it from the Canopy's package manager.
It is likely that the default backend for matplotlib.pyplot is set to qt.
I have very little experience with canopy, but it should be possible to change the default to another back end; maybe someone else will be able to tell you how, or you can find it by yourself.
In the meantime, you can try to add this above all other matplotlib imports:
import matplotlib
matplotlib.use('TkAgg')
From the user guide:
Known Issue: In Canopy running Python 3, GUI backend must be explicitly set
If you ask matplotlib to display a plot, or otherwise run Python code that creates a GUI window, by default you will get an error traceback ending with: ImportError: No module named 'PyQt4'
The solutions are to install PyQt (which is GPL-licensed) in the Canopy Package Manager, or to use the Canopy Preference menu’s Python tab to specify the TK PyLab backend for creating GUIs. For details, see “Python 3 in Canopy 2 - ‘No module named PyQt4’ error”

PyQt5: How to make pyqtgraph work on Debian OS?

I have already installed PyQt5 on my respberry pi 3, and I need pyqtgraph to do live plotting. I have tried to install pyqtgraph using either pip install, or download debian version of pyqtgraph from its official websites, but it just doesn't work. Actually, it is successfully installed, but when I run my program, it just automatically quit without any error message. Also when I comment out the lines related to pyqtgraph, everything goes well. Can someone explain what is wrong with debian/linux version of pyqtgraph?

Categories

Resources