I am trying to complete http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html#using-matplotlib it runs but does not display anything
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
(i am using a raspberry pi and followed this tutorial to install open cv http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ subsequently i pip installed matplotlib)
if i replace plt.show with plt.savefig it works what is wrong?
after adding import matplotlib; matplotlib.use('TkAgg') and import Tkinter or tkinter i get
(cv) pi@raspberrypi:~/Desktop $ python tst4.py
Traceback (most recent call last):
File "tst4.py", line 5, in <module>
from matplotlib import pyplot as plt
File "/home/pi/.virtualenvs/cv/lib/python3.4/site- packages/matplotlib/pyplot.py", line 114, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/home/pi/.virtualenvs/cv/lib/python3.4/site- packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/home/pi/.virtualenvs/cv/lib/python3.4/site- packages/matplotlib/backends/backend_tkagg.py", line 13, in <module>
import matplotlib.backends.tkagg as tkagg
File "/home/pi/.virtualenvs/cv/lib/python3.4/site- packages/matplotlib/backends/tkagg.py", line 9, in <module>
from matplotlib.backends import _tkagg
ImportError: cannot import name '_tkagg'
I've ran into this issue myself. The problem is related to the matplotlib backend not being properly set within the virtual environment. It took me a lot of trial and error, but you first need to install a few dependencies:
$ sudo apt-get install tcl-dev tk-dev python-tk python3-tk
And then manually install matplotlib from source rather than using pip:
$ workon your_env_name
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install
This should take care of the problem.
I detail my full experience and more details to the solution on this page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With