How can I set the 'backend' in matplotlib in Python?
Your currently selected backend, 'agg' does not support show().
AGG
backend is for writing to file, not for rendering in a window. See the backend FAQ at the matplotlib web site.
ImportError: No module named _backend_gdk
For the second error, maybe your matplotlib distribution is not compiled with GTK support, or you miss the PyGTK package. Try to install it.
Do you call the show()
method inside a terminal or application that has access to a graphical environment?
Try other GUI backends, in this order:
TkAgg
wxAgg
Qt5Agg
Qt4Agg
matplotlib: How to create original backend
The most minimal backend could look like this, where we just take the figure canvas from the agg backend (and are hence able to use all associated methods)
from matplotlib.backend_bases import Gcf
from matplotlib.backends.backend_agg import FigureCanvasAgg
FigureCanvas = FigureCanvasAgg
def show(*args, **kwargs):
for num, figmanager in enumerate(Gcf.get_all_fig_managers()):
figmanager.canvas.figure.savefig(f"figure_{num}.png")
If you save this as mybackend.py
, you can use it as backend via matplotlib.use("module://mybackend")
.
import matplotlib
matplotlib.use("module://mybackend")
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,3,2])
plt.figure()
plt.scatter([1,2,3], [3,2,3], color="crimson")
plt.show()
How to change the default backend in matplotlib from 'QtAgg' to 'Qt5Agg' in Pycharm?
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 :
https://matplotlib.org/stable/tutorials/introductory/customizing.html#customizing-with-matplotlibrc-files
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.
Change matplotlib backend in Python virtualenv
You can consider changing your backend to TkAgg
in the Python 2 virtualenv by running the following:
sudo apt install python-tk # install Python 2 bindings for Tk
pip --no-cache-dir install -U --force-reinstall matplotlib # reinstall matplotlib
To confirm the backend is indeed TkAgg
, run
python -c 'import matplotlib as mpl; print(mpl.get_backend())'
and you should see TkAgg
.
Easiest way to install a GUI backend for matplotlib?
I went across methods and found the simplest method: Use TkAgg
backend.
- Install
tkinter
$ sudo apt-get install python-tk
$ sudo apt-get install python3-tk
- Specify this backend in your Python script
import matplotlib
matplotlib.use('TkAgg', force=True)
import matplotlib.pyplot as plt
Now you can freely call plt.show()
for your plots and stay happy :)
How to set matplotlib backend at vscode
Not sure if this is still relevant, but it looks like your integrated terminal is running in headless mode. Changing terminal.integrated.inheritEnv
to true
in settings should fix it.
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. when plotting figure with pyplot on Pycharm
Solution 1: is to install the GUI backend tk
I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest).
All I had to do was to install tkinter
through the Linux bash terminal using the following command:
sudo apt-get install python3-tk
instead of installing it with pip
or directly in the virtual environment in Pycharm.
Solution 2: install any of the matplotlib
supported GUI backends
- solution 1 works fine because you get a GUI backend... in this case the
TkAgg
- however you can also fix the issue by installing any of the matplolib GUI backends like
Qt5Agg
,GTKAgg
,Qt4Agg
, etc- for example
pip install pyqt5
will fix the issue also
- for example
NOTE:
- usually this error appears when you pip install matplotlib and you are trying to display a plot in a GUI window and you do not have a python module for GUI display.
- The authors of
matplotlib
made the pypi software deps not depend on any GUI backend because some people needmatplotlib
without any GUI backend.
Change matplotlib default backend at system level
I've found it... it's the matplotlibrc file. In a virtual environment it is located at venv/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc
Related Topics
Using Pip3: Module "Importlib._Bootstrap" Has No Attribute "Sourcefileloader"
How to Make Python Script Run as Service
Is Everything an Object in Python Like Ruby
How to Merge Two Dictionaries in a Single Expression
Why Is the Order in Dictionaries and Sets Arbitrary
When Should I (Not) Want to Use Pandas Apply() in My Code
Redirect Stdout to a File in Python
Python Regex Find All Overlapping Matches
Check If Multiple Strings Exist in Another String
How to Find All Occurrences of a Substring
How to Change the Environment of a Parent Process in Python
Fail During Installation of Pillow (Python Module) in Linux
Converting Epoch Time With Milliseconds to Datetime
Unboundlocalerror on Local Variable When Reassigned After First Use
Difference Between Re.Search and Re.Match
How to Use Threading in Python