Find first instance of package import in Python - python

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

Related

Matplotlib has a problem finding a backend after I use pyinstaller

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.)

backend use from matplotlib

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?

What GUI library is matplotlib using?

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

What is the difference between importing matplotlib and matplotlib.pyplot?

I'm still fairly new to python and am wondering if the x.y statement means y is a submodule of x? And if so, doesn't the command:
import matplotlib.pyplot as plt
only import this particular submodule and nothing else? I had to do this in order to get access to the hist function. How does that affect the modules normally imported when calling import matplotlib as plt? Can I get all the modules in matplotlib together under the plt name?
I'm aware that this question is related to what is the difference between importing python sub-modules from NumPy, matplotlib packages But the answer in this question does not tell me if nothing else in matplotlib is imported and how to just import all of matplotlib without worrying about submodules being left out.
Have a look at this codebase tree: matplotlib contains a library of code, while pyplot is only a file of this lib.
import matplotlib
will imports all the files inside this repo. For example to use it:
import matplotlib as mpl
mpl.pyplot.plot(...)
To import pyplot:
from matplotlib import pyplot as plt
# or
import matplotlib.pyplot as plt
plt.plot(...)
One question for you: what console do you use? I guess it's Ipython console or something?
Edit:
To import all:
from matplotlib import *
pyplot(...)
Why do I guess you are using Ipython? Ipython console imports all modules from numpy and some other libraries by default on launch, so that in Ipython console you can simple use: sqrt, instead of import math; math.sqrt, etc. matplotlib is imported in Ipython be default.
I don't know of any way to import all the functions from every submodule. Importing all the functions from a submodule is possible the way you suggested with e.g. from matplotlib.pyplot import *.
Be noted of a potential problem with importing every function; you may override imported functions by defining your own functions with the same name. E.g:
from matplotlib.pyplot import *
def plot():
print "Hello!"
plot()
would output
Hello!
I had conda installed, which has added stuff to ~/.bashrc.
Commenting that made it work for me.

spyder matplotlib UserWarning: This call to matplotlib.use() has no effect because the backend has already been chosen

So I'm trying to write a piece of code that creates a figure, but in order for it to run on the computer that I want it to (school one), I can't use the x-window backend to create the figure. I tried switching the backend use (I have the matplotlib.use('Agg') statement in my code), but whenever it creates the figure, it gives me 3 colorbars on the figure when I just want one. It also gives me the error
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.
warnings.warn(_use_error_msg)
My code starts like this:
import matplotlib
matplotlib.use('Agg')
from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
Any ideas or help?
Late reply, but I wanted to document this for anyone else.
You're likely using Spyder as your python interface. When you start Spyder it automatically loads those modules, hence the error about how it's already been chosen. There's a link here that explains it: https://groups.google.com/forum/#!topic/spyderlib/tRwgqEAIyvs

Categories

Resources