Recently we've started to see this error appear:
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
warnings.warn(_use_error_msg)
/var/build/user_builds/astropy/checkouts/latest/docs/analytic_functions/index.rst:83: WARNING: Exception occurred in plotting index-1
from /var/build/user_builds/astropy/checkouts/latest/docs/analytic_functions/index.rst:
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/matplotlib/sphinxext/plot_directive.py", line 515, in run_code
exec code in ns
File "<string>", line 14, in <module>
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1046, in subplots
fig = figure(**fig_kw)
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 423, in figure
**kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
window = Tk.Tk()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1767, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: no display name and no $DISPLAY environment variable
when using the matplotlib plot_directive. It seems something has changed in the RTD configuration and the backend is defaulting to something interactive.
(here's the Astropy issue where someone first reported this: https://github.com/astropy/astropy/issues/3600)
@ericholscher - any idea what could be causing this? All our plots are currently broken due to this issue, which seems beyond our control and related to RTD. Are other projects not having this issue?
We haven't changed anything that I know of. Did you try this workaround?
http://stackoverflow.com/a/3054314
On Tue, Mar 24, 2015 at 1:48 AM, Thomas Robitaille <[email protected]
wrote:
@ericholscher https://github.com/ericholscher - any idea what could be
causing this? All our plots are currently broken due to this issue, which
seems beyond our control and related to RTD. Are other projects not having
this issue?—
Reply to this email directly or view it on GitHub
https://github.com/rtfd/readthedocs.org/issues/1195#issuecomment-85406365
.
Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com
@ericholscher - yes, we already call this but we see:
/usr/lib/pymodules/python2.7/matplotlib/__init__.py:1173: 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.
which suggests that the backend has already been set beforehand.
Any suggestions? I don't know much about matplotlib, and we haven't done
anything that I know should effect it. I'm happy to upgrade it, it seems
we're using 1.3.1, but I don't really know how to debug issues since I
don't know the software at all.
On Tue, Mar 24, 2015 at 9:01 AM, Thomas Robitaille <[email protected]
wrote:
@ericholscher https://github.com/ericholscher - yes, we already call
this but we see:/usr/lib/pymodules/python2.7/matplotlib/init.py:1173: 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.which suggests that the backend has already been set beforehand.
—
Reply to this email directly or view it on GitHub
https://github.com/rtfd/readthedocs.org/issues/1195#issuecomment-85576963
.
Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com
Maybe @mdboom has some ideas - I'm also a bit puzzled by the behavior currently. Basically I don't understand how it ever changes to use TkAgg (since it seems that default on RTD is Agg).
Ah, sorry, I think I have it. Check out this minimalist project:
https://github.com/astrofrog/test-rtd
The RTD log (https://readthedocs.org/builds/test-rtd-astrofrog/2482239/) shows:
Making output directory...
Running Sphinx v1.2.2
TkAgg
so the default backend is set to TkAgg.
@mdboom, do you have any suggestions on how to make sure the default backend is Agg, not TkAgg, system-wide? There is no reason the TkAgg backend should ever be needed on RTD.
Looks like there's a config in /etc/matplotlibrc that sets it.
On Tue, Mar 24, 2015 at 9:14 AM, Thomas Robitaille <[email protected]
wrote:
Ah, sorry, I think I have it. Check out this minimalist project:
https://github.com/astrofrog/test-rtd
The RTD log (https://readthedocs.org/builds/test-rtd-astrofrog/2482239/)
shows:Making output directory...
Running Sphinx v1.2.2
TkAggso the default backend is set to TkAgg.
@mdboom https://github.com/mdboom, do you have any suggestions on how
to make sure the default backend is Agg, not TkAgg, system-wide?—
Reply to this email directly or view it on GitHub
https://github.com/rtfd/readthedocs.org/issues/1195#issuecomment-85586195
.
Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com
@ericholscher - ah great, can you change it to Agg? There's no reason it should be anything else on a display-less server like RTD. Then no one will need to use the matplotlib.use workaround.
@ericholscher - it seems to be working now, did you make the change? If so, this issue can be closed :)
Yea, should be changed. Will keep this open because it needs to be fixed in
our deployment scripts as well, so it doesn't break again when we rev a new
build server.
On Tue, Mar 24, 2015 at 10:53 AM, Thomas Robitaille <
[email protected]> wrote:
@ericholscher https://github.com/ericholscher - it seems to be working
now, did you make the change? If so, this issue can be closed :)—
Reply to this email directly or view it on GitHub
https://github.com/rtfd/readthedocs.org/issues/1195#issuecomment-85620289
.
Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com
Believe this should be fixed.
Hello, i meet the same problem
"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."
I am wondering that how to configure /etc/matplotlibrc to solve this problem.
Thanks !!!
Hi anyone could comment how to trouble shoot this issue. how to configure /etc/matplotlibrc to solve this problem.
Well putting these lines at the beginning of the Py code solves the purpose:
import matplotlib
matplotlib.use('Agg')
Hi,
this didn't worked for me
import matplotlib
matplotlib.use('Agg')
so instead I used:
import matplotlib.pyplot as plt
plt.switch_backend('agg')
that worked. maybe it's helpful to someone else.
Hello, I am working in spyder, in @windows, and this FINALLY worked for me:
Go to Tools>Preferences>IPython terminal>Graphs (Menu names are an approximation since my spyder is in Spanish)
Once here, uncheck the box "Activate support".
Reestart spyder and then type:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
a = [1, 2, 3, 4]
b = [2, 4, 6, 8]
plt.plot(a, b)
I hope this helps you.
Could it be that this problem resurfaced in the Python 3.6 image? Since it get a
No module named '_tkinter'
for a module importing matplotlib with the latest image.
@Tillsten did you try specifying that requirement on your requirements.txt file? If you still think the problem is rtd related, please open a new issue with your rtd project link.
Yes, I did. The problem seems to be identical to this issue: matplotlib is installed, but the default backend is set to tkagg (using tkinter), which is not available on the headless rtd-image. This problem can be fixed by changing the rc file like above or upgrading matplotlib to 3.0, which should fix that issues.
My current workaround is just import and setting the matplotlib backend in the sphinx conf.py :
import matplotlib
matplotlib.use('agg')
Thanks for sharing the workaround! I'll take a look to the docker images later
I didn't see anything else on the docker images related to this, so, not sure how this was solved (maybe rtd wasn't using the docker images in that time?). We have a workaround, so, not sure if we should reopen. Maybe adding this to the faq?
Most helpful comment
Hi,
this didn't worked for me
import matplotlib
matplotlib.use('Agg')
so instead I used:
import matplotlib.pyplot as plt
plt.switch_backend('agg')
that worked. maybe it's helpful to someone else.