Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib backend should be forced to Agg #1195

Closed
astrofrog opened this issue Mar 17, 2015 · 22 comments
Closed

Matplotlib backend should be forced to Agg #1195

astrofrog opened this issue Mar 17, 2015 · 22 comments
Assignees
Labels
Operations Operations or server issue

Comments

@astrofrog
Copy link

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: astropy/astropy#3600)

@astrofrog
Copy link
Author

@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?

@ericholscher
Copy link
Member

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 <notifications@github.com

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
#1195 (comment)
.

Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com

@astrofrog
Copy link
Author

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

@ericholscher
Copy link
Member

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 <notifications@github.com

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
#1195 (comment)
.

Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com

@astrofrog
Copy link
Author

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

@astrofrog
Copy link
Author

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.

@ericholscher
Copy link
Member

Looks like there's a config in /etc/matplotlibrc that sets it.

On Tue, Mar 24, 2015 at 9:14 AM, Thomas Robitaille <notifications@github.com

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
TkAgg

so 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
#1195 (comment)
.

Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com

@astrofrog
Copy link
Author

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

@astrofrog
Copy link
Author

@ericholscher - it seems to be working now, did you make the change? If so, this issue can be closed :)

@ericholscher
Copy link
Member

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 <
notifications@github.com> 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
#1195 (comment)
.

Eric Holscher
Maker of the internet residing in Portland, Or
http://ericholscher.com

@ericholscher ericholscher added Admin Only Operations Operations or server issue and removed Admin Only labels Mar 24, 2015
@ericholscher
Copy link
Member

Believe this should be fixed.

@Newsteinwell
Copy link

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 !!!

@ksaha79
Copy link

ksaha79 commented Sep 27, 2017

Hi anyone could comment how to trouble shoot this issue. how to configure /etc/matplotlibrc to solve this problem.

@ksaha79
Copy link

ksaha79 commented Sep 27, 2017

Well putting these lines at the beginning of the Py code solves the purpose:
import matplotlib
matplotlib.use('Agg')

@DemetDemir1
Copy link

DemetDemir1 commented Mar 1, 2018

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.

@leticiaram
Copy link

leticiaram commented Mar 3, 2018

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

#Example of plot saved to pdf file
a = [1, 2, 3, 4]
b = [2, 4, 6, 8]
plt.plot(a, b)
plt.savefig('test.pdf')

I hope this helps you.

@Tillsten
Copy link

Tillsten commented Sep 29, 2018

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.

@stsewd
Copy link
Member

stsewd commented Oct 1, 2018

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

@Tillsten
Copy link

Tillsten commented Oct 2, 2018

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.

@Tillsten
Copy link

Tillsten commented Oct 2, 2018

My current workaround is just import and setting the matplotlib backend in the sphinx conf.py :

 import matplotlib
 matplotlib.use('agg')

@stsewd
Copy link
Member

stsewd commented Oct 2, 2018

Thanks for sharing the workaround! I'll take a look to the docker images later

@stsewd
Copy link
Member

stsewd commented Oct 2, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Operations Operations or server issue
Projects
None yet
Development

No branches or pull requests

9 participants