I am creating a module where I use tensorflow object detection api. Well, I came up with the situation where the original tensorflow code used:
import matplotlib; matplotlib.use('Agg')
but pyplot was previously used in my code so the backend was automatically set to TkAgg as I can guess from this message:
Backend TkAgg is interactive backend. Turning interactive mode on.
From the documentation of matplotlib about backends I didn't get the difference between them. It seems that Agg is the general rendering engine and TkAgg is the backend (render+canvas)?
I want to disable the warning (by eliminating the reason not by suppressing it) and I was wondering if it makes any difference to specify
matplotlib.use('Agg')
rather than
matplotlib.use('TkAgg')
To my understanding the former seems more general and I would guess that a renderer (Agg, GTK, Cairo etc) is being chosen automatically? Is this the case? So, in my system (Ubuntu 16.04) they are equivalent? Does the former provide more portability to other platforms while the latter is stuck with Tk for example?
Related
I'm trying to make an .exe file from .py and I'm having an issue with matplotlib library. When I run my script in VS Code plt.show() works just fine. But when I use pyinstaller to make an .exe and run the .exe file, it gives me a following warning: "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." Importing TkAgg directly with "import matplotlib.backends.backend_tkagg" doesn't solve the problem either.
Is it possible to fix that?
I ran into the same issue.
I found this article, which may be helpful:
"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm
However, the simple fix in my case was to just explicitly import the backend module(s):
import matplotlib.backends.backend_tkagg
import matplotlib.backends.backend_wxagg
Pyinstaller does some relatively sophisticated analysis and Matplotlib itself has some relatively sophisticated code to be able to use multiple GUI backends. I think the overall problem is that Pyinstaller isn't seeing that any particular backend is actually being used and so doesn't end up bundling any backend module.
Explicitly making the import makes it obvious to Pyinstaller that you want that module (importing it is effectively using it). (Maybe you only need the one for 'tkagg' - I happen to be working with a WX GUI, so I included both imports - it works.)
I need to set the backend for matplotlib in order to apply this solution for addressing issues with non-thread-safe code in Tkinter and using the Tk backend in matplotlib. According to the post I need to do
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
but setting the backend must be done before any imports of pyplot. I thought I found the first import, but I keep getting this warning:
/usr/lib/python2.7/dist-packages/matplotlib/__init__.py:1352: UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.
My codebase is somewhat large and complex, so it's becoming a pain for me to try to track down where the very first import of pyplot is. Is there a way to test this programmatically, or is there a debug tool I can use to figure this out?
Try matplotlib.use('Agg',warn=False,force=True). This might be duplicate of How to switch backends in matplotlib / Python
When trying to import pyplot from matplotlib, I get this error (python 3.6). It works fine on my other computer, which has python 3.5, but I don't think it changed much.
I've tried both:
from matplotlib import pyplot as plt
and
import matplotlib.pyplot as plt
It's really annoying me that I can't figure this out.
matplotlib targets many different use cases and output formats. Some people use matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people embed matplotlib into graphical user interfaces like wxpython or pygtk to build rich applications. Others use matplotlib in batch scripts to generate postscript images from some numerical simulations, and still others in web application servers to dynamically serve up graphs.
To support all of these use cases, matplotlib can target different outputs, and each of these capabilities is called a backend; the “frontend” is the user facing code, i.e., the plotting code, whereas the “backend” does all the hard work behind-the-scenes to make the figure. There are two types of backends: user interface backends (for use in pygtk, wxpython, tkinter, qt4, or macosx; also referred to as “interactive backends”) and hardcopy backends to make image files (PNG, SVG, PDF, PS; also referred to as “non-interactive backends”).
In yout case you have to choose backend "Agg" to use pyplot.
Solution:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
reference:
https://matplotlib.org/tutorials/introductory/usage.html#what-is-a-backend
https://github.com/matplotlib/matplotlib/issues/9954
Let me guess:
1:Maybe you have not properly installed matplotlib, you can try this:
pip3.6 install --upgrade matplotlib
Or just deleted the package and reinstall
2:Check whether you have use the same config environment and execution environment:
In some cases, you have installed packages for your local python interpreter, but you actually run your program on python virtual env. Maybe you have not properly installed the package on virtual env.
By the appearance of matplotlib, I guess it is using PyQt; but when I looked into the code of it, I didn't find it importing any GUI library at all. So, what exactly does matplotlib use to show the graph (for example, the window when calling plt.show())?
You can tell which backend matplotlib is using with the get_backend() method:
matplotlib.get_backend()
You can see a list of all available backends with rcsetup.all_backends:
matplotlib.rcsetup.all_backends
I am new to python and trying to explore graphs, could you please help me understand if I can plot graphs on the console using matplotlib on a linux system that has no XSERVER running?
Thanks.
You can use matplotlib with no X server by setting the backend to Agg, PS, PDF or SVG, Cairo or GDK (depending on what kind of file you wish to create). You can set the backend in your matplotlibrc file, which, depending on your installation, may be in a directory such as ~/ or ~/.matplotlib or ~/.config/matplotlib/.
Alternatively, you can set the backend in the script itself.
Be sure to set the backend first, before importing other modules such as pyplot:
import matplotlib
matplotlib.use("Agg")
See this SO question for examples.