1

I am having trouble getting my matplotlib library to work. I am running it on a virual environment, and I have installed numpy, and matplotlib. I first open my virtual environment, activate python, then I enter,

import matplotlib 

everything goes fine, but then I enter,

import matplotlib.pyplot as plt

I get the error,

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/Users/kendallreid/.virtualenvs/cv/lib/python2.7/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/Users/kendallreid/.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 <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

I have tried a number of different solutions to no avail. What could be the problem?

2
  • for python < 3.1 -> pip install tkinter - You may want to check stackoverflow.com/q/20044559/797495 Commented Apr 23, 2017 at 15:51
  • 1
    Try this : import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt Commented Apr 23, 2017 at 15:58

1 Answer 1

2

Use the below mentioned commands:

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

This worked for me. I hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.