In a Python module where I use matplotlib
, I want to make sure it works also when I run the script on a remote machine via ssh
. So I do:
import matplotlib
matplotlib.use('Agg')
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np
import pylab
import scipy.stats
import scipy.stats.mstats
It works. Too bad that when I run it directly on a machine (not a remote one!), it gives me the following warning:
This call to matplotlib.use() has no effect because the the backend has already been chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.
How do I remove this message?
While I can't test this Ipython tells me that "one can set warn=False to supporess the warnings."
Source:
Always fun to find a typo in the docs.
Warning messages are usually significant, and I recommend not ignoring. I found your question while searching for a solution to my doc build with sphinx. I received a similar message, and some additional context for the warning:
I then found a solution at https://github.com/conchoecia/pauvre/issues/18 . With the import order as follows:
Before the fix I only had the following import for the module
I am thinking the import order for this question resulted in the warning message. However, I was not able to recreate your warning for the information provided. It would have been nice to see a couple more lines from that warning.
After some more discussions with other developers, it became apparent my import of pyplot was in the file whereas it belongs in the module just where I need to use plt.
Understanding the render is important, and you can get more at https://matplotlib.org/faq/usage_faq.html#what-is-a-backend and https://matplotlib.org/api/matplotlib_configuration_api.html#matplotlib.use Just remember other proceeding code may be changing or defaulting the backend names.