Qt5Agg
is necessary to use the mayavi
3D visualization package. I have installed PyQt5
and mayavi
using pip in a separate copied conda
environment. The default backend then changes from TkAgg
to QtAgg
. This is a bit weird because in an earlier installation in a different PC the default changed directly to Qt5Agg
. I always check the backend using the following commands from the python console :
import matplotlib matplotlib.get_backend()
Even with the backend being ‘QtAgg’, I am able to use mayavi
from the terminal without any issue but not when I do so in Pycharm. Here I get a non-responsive empty window (image below) :
Image of the non-responsive window
I have been able to get rid of this issue by explicitly using Qt5Agg
instead of QtAgg
before the plt
call :
import matplotlib matplotlib.use('Qt5Agg') import matplotlib.pyplot as plt
But I would prefer a better way than using the above in every script that I write. As I had mentioned earlier, I already have mayavi installed and have used it successfully it in Pycharm in a different PC and there the default backend is ‘Qt5Agg’ and hence there is no need to change the backend explicitly.
Is there anything obvious that I’m overlooking ? Can you please let me know of a way to change the default backend for matplotlib from QtAgg
to Qt5Agg
after PyQt5
installation using pip ?
Thanks in advance !!
Advertisement
Answer
Thanks to @PaulH’s comment, I was able to solve the issue. Owing to @mx0’s suggestion, I shall now explicitly mention the fix below so that others can also benefit from it.
In a particular conda environment, if matplotlib
package is installed, then there will be a ‘matplotlibrc’ file stored somewhere that defines what the default backend will be whenever matplotlib
is imported from that conda environment. The location of this ‘matplotlibrc’ can be found using the following commands :
import matplotlib matplotlib.matplotlib_fname()
Please look into the following link if there’s any deprecation issue with the above commands :
Once the location of the ‘matplotlibrc’ file is known, open it and simply uncomment one line inside this file. Just change the backend from :
##backend: Agg
to :
backend: Qt5Agg
And that’s it. All the plot window troubles in PyCharm will be solved as far as the mayavi
3D visualization package is concerned. For any other use, where a specific backend is necessary, you can also set the default to any other backend of choice.