How to Change Backends in Matplotlib/Python

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

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

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.

What is the default GUI backend for Matplotlib?

The backend selection logic is not very transparent and not well documented.

In modern matplotlib there is no "default backend", i.e. the rcParams['backend'] is set to a "sentinel".

Upon importing matplotlib the first working backend from a candidate list ["macosx", "qt5agg", "qt4agg", "gtk3agg", "tkagg", "wxagg"] is chosen.

In order to avoid this automatic selection, you can set the backend manually via the rcParams['backend'] parameter or the MPLBACKEND environment variable. That part is documented

Change default backend for matplotlib in Jupyter Ipython

The question is similar to Automatically run %matplotlib inline in IPython Notebook, except that you want to automatically use TK backend instead of inline backend.

So the idea is to locate you IPython configuration file. See configure IPython. It should be

/.ipython/profile_default/ipython_kernel_config.py

If it doesn't yet exist, create it via > ipython profile create.

Inside this file locate the setting c.InteractiveShellApp.matplotlib and set it to "tk". It should then look like

## Configure matplotlib for interactive use with the default matplotlib backend.
c.InteractiveShellApp.matplotlib = "tk"

Save the file and restart the kernel.



Related Topics



Leave a reply



Submit