0
votes

I'm new to Python and programming. I was trying to "import matplotlib.pyplot as plt" in my program but Python gives me the below error:

Traceback (most recent call last): File "img_proc_canny_edge_detection.py", line 3, in import matplotlib.pyplot as plt File "/Users/alireza/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in _backend_mod, new_figure_manager, draw_if_interactive, _show = >pylab_setup() File "/Users/alireza/.virtualenvs/cv/lib/python2.7/site->packages/matplotlib/backends/init.py", line 32, in pylab_setup globals(),locals(),[backend_name],0) File "/Users/alireza/.virtualenvs/cv/lib/python2.7/site->packages/matplotlib/backends/backend_tkagg.py", line 6, in from six.moves import tkinter as Tk File "/Users/alireza/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 203, in load_module mod = mod._resolve() File "/Users/alireza/.virtualenvs/cv/lib/python2.7/site-packages/six.py", >line 115, in _resolve return _import_module(self.mod) File "/Users/alireza/.virtualenvs/cv/lib/python2.7/site-packages/six.py", >line 82, in _import_module import(name) File >"/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/>lib/python2.7/lib-tk/Tkinter.py", line 39, in import _tkinter # If this fails your Python may not be configured for >Tk ImportError: No module named _tkinter

when I comment out "import matplotlib.pyplot as plt" line from my program, it works perfectly.

note: I've already installed matplotlib

1
How did you install python and matplotlib? Did you try installing tkinter or changing the backend?Thomas Kühn

1 Answers

0
votes

Use the following lines of code and it should work in virtual environment without any issues

import matplotlib
matplotlib.use(‘TkAgg’) 
import matplotlib.pyplot as plt