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

Using matplotlib without Tkinter #7115

Closed
TheChymera opened this Issue Sep 15, 2016 · 10 comments

Comments

Projects
None yet
6 participants
@TheChymera

TheChymera commented Sep 15, 2016

I am using matplotlib on a headless server, so I don't have or want any GUI support.

Recently (since about 3 days ago, no what this update may have coincided with) I have become unable to import pyplot, due to matplotlib requiring Tkinter:

    import matplotlib.pyplot as plt
  File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 114, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from matplotlib.externals.six.moves import tkinter as Tk
  File "/usr/lib64/python2.7/site-packages/matplotlib/externals/six.py", line 199, in load_module
    mod = mod._resolve()
  File "/usr/lib64/python2.7/site-packages/matplotlib/externals/six.py", line 113, in _resolve
    return _import_module(self.mod)
  File "/usr/lib64/python2.7/site-packages/matplotlib/externals/six.py", line 80, in _import_module
    __import__(name)
ImportError: No module named Tkinter

I believe some other change in my system exposed this long hidden issue, and pyplot has always required Tkinter- is it possible to change that?


  • Matplotlib-1.5.3 Python-2.7.12 running on Gentoo Linux
  • Installed from source via Portage
@tacaswell

This comment has been minimized.

Member

tacaswell commented Sep 15, 2016

You just need to set the backend to be 'agg' using one of the methods documented at http://matplotlib.org/faq/usage_faq.html#what-is-a-backend.

If you can not get this to work again, please send an email to the user mailing list (matplotlib-user@python.org / https://mail.python.org/mailman/listinfo/matplotlib-users you will have to join to post un-moderated).

@cybertk

This comment has been minimized.

cybertk commented Apr 3, 2018

@tacaswell 's solution works well, just need a export MPLBACKEND=agg

@tacaswell

This comment has been minimized.

Member

tacaswell commented Apr 3, 2018

To future readers of this thread: Where would you have expected to have found this documented?

@ohcibi

This comment has been minimized.

ohcibi commented Apr 22, 2018

@tacaswell TL;DR: right in the installation instructions.

This is a typical issue with documentation about software which can be used with different sub libraries especially in the linux world.

Its absolutely correct to link the theory about "What is a backend?" from the installation instruction as the full theory is not necessary for those instructions. However that doesn't mean that the installation instruction should not tell anything at all about the different possibilities and how to deal with it as the installation instructions is meant for a user that doesn't have a clue about all this stuff. So what he needs is some sane default path to go with. Apart from the theory everybody already knows that a "backend" would be something that can be switched and eventually needs to be decided on, so please tell me right away what I should choose for my first steps with this tool/product/software and do not hide that behind some wall of text which I need to click a link before even seeing.

If there ever is two choices that cannot clearly compared to each other regarding which is the sanest one, the instruction should explain very precisely and briefly what the difference is, what the recommended approach for either path is and if necessary also how to determine what path the reader is currently one as you cannot assume any of that.

@andyfaff

This comment has been minimized.

Contributor

andyfaff commented May 16, 2018

I'm finding the documentation slightly confusing on this point. The documentation suggests the following:

export MPLBACKEND="module://my_backend"

However, I don't think this example is clear. I tried:

export MPLBACKEND="module://AGG"

and it didn't work. Am I supposed to use something else for module? What about my_backend? It's probably better to give an example string.

@tacaswell

This comment has been minimized.

Member

tacaswell commented May 16, 2018

@andyfaff That is if you want to provide a custom backend and should be a full import path. To using Agg you want

export MPLBACKEND=Agg
@tacaswell

This comment has been minimized.

Member

tacaswell commented May 16, 2018

@ohcibi Could you open a PR that makes those changes?

@andyfaff

This comment has been minimized.

Contributor

andyfaff commented May 16, 2018

export MPLBACKEND=Agg

That would have saved me a couple of hours yesterday.

@ohcibi

This comment has been minimized.

ohcibi commented May 17, 2018

@tacaswell Idk if I'm the right person for that. I was replying to your question where that documentation should be placed but I'm unsure If I know 100% on what that documentation should tell. Maybe you could provide me a brief step-by-step list for a very simple example that plots f(x)=x into a png file on a clean system. e.g. in form of a Dockerfile that uses an alpine-linux image and its CMD does the plotting if youre familiar with docker. If you dont know docker just assume an ubuntu-minimal and write a bash script that does the plotting when run as root (just to be able to install dependencies to make it super dirty but simple). Maybe I can derive a documentation from that.

@andrewcooke

This comment has been minimized.

andrewcooke commented Oct 4, 2018

To future readers of this thread: Where would you have expected to have found this documented?

it would be nice if the comment that appeared in the stack trace said this instead of (as currently) "# If this fails your Python may not be configured for Tk"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment