You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just wanted to use matplotlib to draw a line graph and save it to a file, but every time I try to import the pyplot module I get the abovementioned error message.
python3 -c "import matplotlib.pyplot"
Actual outcome
>>> import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/usr/local/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/usr/local/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
from six.moves import tkinter as Tk
File "/usr/local/lib/python3.6/site-packages/six.py", line 92, in __get__
result = self._resolve()
File "/usr/local/lib/python3.6/site-packages/six.py", line 115, in _resolve
return _import_module(self.mod)
File "/usr/local/lib/python3.6/site-packages/six.py", line 82, in _import_module
__import__(name)
File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
Expected outcome
Whatever happens when import matplotlib.pyplot succeeds
Matplotlib version
Operating System: Windows Subsystem for Linux
Matplotlib Version: 2.0.2
Python Version: 3.6.0
I installed matplotlib using pip.
Doing sudo apt-get install python3-tk, as recommended by someone on Stack Overflow, didn't help.
If you built from source suspect you also need the tk dev packages as well?
This is the first report (that I can recall) of using Matplotilb in Windows Subsystem for Linux, you may be a guinea pig 😅 Any notes from you experience with this that could go in the docs would be very appreciated!
If you didn't install tk in your python then you can not use the tk backend.
Reacted by James Xue, Syed Mohammad Safdar Ali, Vidal González-Pereira, mason, kakashiS, wangvation, Ray Phan, Osman Sarood, MilkWay, Hai Tran and 58 more
Reacted by Manish, Francesco Belvedere, lucidsushi, Michael Pang, Enes Malik Turhan, Alexei Matusevski, Kevin Prichard, Rody Arantes, Carl Davidson, TanmayNakhate and 1 more
Reacted by wangvation, Osman Sarood, Bernardo Modenesi, Mohit Rathore, 胡小根, Biki Deka, catindog/nardtree, mao yong, Laurent Berder, Xu Jun and 16 more
Reacted by Francesco Belvedere, lucidsushi, Michael Pang, Enes Malik Turhan, Alexei Matusevski, Ryan Loi, Kevin Prichard, Rody Arantes, TanmayNakhate, youngmarl and 4 more
Closing as this is apparently a python install issue -- but feel free to request a reopen if something else is wrong, or to suggest some change in the docs.
My 2c: should be reopened for the following reason:
from matplotlib import pyplot as plt
ModuleNotFoundError: No module named '_tkinter'
import matplotlib
matplotlib.use("agg")
UserWarning:
This call to matplotlib.use() has no effect...
from matplotlib import pyplot as plt
ModuleNotFoundError: No module named '_tkinter'
First error is fine, but the second error is not.
IMO it's not OK that state is messed up and user cannot recover.
The issue of pyplot setting the backend is currently being worked on from
multiple angles. This has been a long-standing issue, and it is a very
tricky issue to address. One of the first features that'll likely come out
of it is configuring matplotlib by default to respect a hierarchy of
backend choices, so it can fail-over more gracefully to other backends.
importmatplotlibmatplotlib.use('agg')
importmatplotlib.pyplotasplt# no error, it worksplt.figure(1).show()
arisen:
/root/venv36/lib/python3.6/site-packages/matplotlib/figure.py:418: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "
It's a total bummer that matplotlib initializes the backend early, and fails hard.
Perhaps there's a good reason for that.
From what I see, a notable share of users stumbles on this when trying matplotlib for the first time, i.e. if they copy-paste an example and boom! a cryptic exception instead of pretty plots.
I sincerely hope that the maintainers take a second look at this issue.
From what I see, a notable share of users stumbles on this when trying matplotlib for the first time, i.e. if they copy-paste an example and boom! a cryptic exception instead of pretty plots.
The other option is no exceptions and no plots. The cryptic error at least suggests that something is not behaving as expected.
@jorijnsmit We do not have a hard dependency on an GUI toolkit because there are many uses where you do not need it (batch jobs, use in a notebook, in a webserver etc).
The dependency on Tk is further complicated because the bindings are part of the standard library, but some packagers split it out into a separate package. If you think the Matplotliblib package should depend on the python-tk package, please take that up with the maintainers of your packaging tool.
I know this is an old issue, but I found the most reliable solution to this on macOS, especially in combination with the PyCharm debugger crashing due to matplotlib related tk issues, was to use the backend 'macosx'.
Reacted by Nick, A. Craze, José de la Campa and darkerMatter
Activity
tacaswell commentedon Aug 10, 2017
How did you install mpl?
Can you do
import tkinterin a python shell?If you built from source suspect you also need the tk dev packages as well?
This is the first report (that I can recall) of using Matplotilb in Windows Subsystem for Linux, you may be a guinea pig 😅 Any notes from you experience with this that could go in the docs would be very appreciated!
SamusAranX commentedon Aug 10, 2017
😛
I installed Python 3.6 from source though, omitting the tk packages as I didn't need them.
Guess I'll try that tomorrow while upgrading to 3.6.2.
tacaswell commentedon Aug 10, 2017
Oh, that is easy to fix then
If you didn't install tk in your python then you can not use the tk backend.
WeatherGod commentedon Aug 11, 2017
anntzer commentedon Aug 17, 2017
Closing as this is apparently a python install issue -- but feel free to request a reopen if something else is wrong, or to suggest some change in the docs.
dimaqq commentedon Dec 7, 2017
My 2c: should be reopened for the following reason:
First error is fine, but the second error is not.
IMO it's not OK that state is messed up and user cannot recover.
jklymak commentedon Dec 7, 2017
Pyplot sets the backend if it’s lot been set already. The user only need move the import matpltolib and matplotlib.use before the pyplot import.
dimaqq commentedon Dec 7, 2017
That's exactly the problem -- requiring user to do something before they know they need it.
WeatherGod commentedon Dec 7, 2017
shenjiefeng commentedon Feb 23, 2018
on minimal install centos7.
first
then in ipython env,
arisen:
/root/venv36/lib/python3.6/site-packages/matplotlib/figure.py:418: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
"matplotlib is currently using a non-GUI backend, "
I have already Configure X11 forwarding on xshell with the steps of http://www.doxer.org/use-xming-xshell-putty-tightvnc-to-display-linux-gui-on-windows-desktop-x11-forwarding-when-behind-firewall/
but it still not work
tacaswell commentedon Feb 23, 2018
@shenjiefeng with
matplotlib.use('agg')you are explicitly asked for a non-GUI backend.This question would be better handled on the mailing list [email protected] (https://mail.python.org/mailman/listinfo/matplotlib-users you will have to subscribe to post un-moderated).
fixed matplotlib import error for systems without TkInter installed.
CarlWilson111 commentedon Sep 18, 2018
That is used for me.
In some Saas platforms, users are forbiden to change system environment, so fix like this will help.
Thank you very much.
dimaqq commentedon Sep 18, 2018
It's a total bummer that matplotlib initializes the backend early, and fails hard.
Perhaps there's a good reason for that.
From what I see, a notable share of users stumbles on this when trying matplotlib for the first time, i.e. if they copy-paste an example and boom! a cryptic exception instead of pretty plots.
I sincerely hope that the maintainers take a second look at this issue.
jklymak commentedon Sep 18, 2018
3.0.0 will change the backend fallback behaviour.
tacaswell commentedon Sep 18, 2018
The other option is no exceptions and no plots. The cryptic error at least suggests that something is not behaving as expected.
dimaqq commentedon Sep 19, 2018
Thanks @jklymak that's very good to hear!
sajed-zarrinpour commentedon Apr 11, 2019
in my case on manjaro Linux, installing python-pmw worked it out.
Comment out usages of matplot, which cause an exception when running …
sajed-zarrinpour commentedon Nov 10, 2019
KipCrossing commentedon May 29, 2020
lt;dr?
upgrade to >3.0.0
gosuto-inzasheru commentedon Jun 2, 2020
I am still getting this on
matplotlib=3.2.1. Also, why istkinternot just a dependency ofmatplotlib? This is messing up package management for me.jklymak commentedon Jun 2, 2020
@jorijnsmit Can you open a new issue that explains what you are trying to do. w/ 3.2.1 you should get a fallback to
aggat the very least.tacaswell commentedon Jun 2, 2020
@jorijnsmit We do not have a hard dependency on an GUI toolkit because there are many uses where you do not need it (batch jobs, use in a notebook, in a webserver etc).
The dependency on Tk is further complicated because the bindings are part of the standard library, but some packagers split it out into a separate package. If you think the Matplotliblib package should depend on the python-tk package, please take that up with the maintainers of your packaging tool.
phoenixeliot commentedon Sep 16, 2020
I know this is an old issue, but I found the most reliable solution to this on macOS, especially in combination with the PyCharm debugger crashing due to matplotlib related tk issues, was to use the backend
'macosx'.