gpt4 book ai didi

python - matplotlib 运行但不生成图形

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:48 24 4
gpt4 key购买 nike

我正在尝试完成 http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html#using-matplotlib它运行但不显示任何内容

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()

(我正在使用树莓派并按照本教程安装 open cv http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ 随后我 pip 安装了 matplotlib)

如果我用 plt.savefig 替换 plt.show 它会起作用,有什么问题吗?


添加import matplotlib之后; matplotlib.use('TkAgg')import Tkintertkinter 我得到

    (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'

最佳答案

我自己也遇到过这个问题。该问题与虚拟环境中未正确设置 matplotlib 后端有关。我试了很多次,但你首先需要安装一些依赖项:

$ sudo apt-get install tcl-dev tk-dev python-tk python3-tk

然后手动从源代码而不是使用 pip 安装 matplotlib:

$ workon your_env_name
$ pip uninstall matplotlib
$ git clone https://github.com/matplotlib/matplotlib.git
$ cd matplotlib
$ python setup.py install

这应该可以解决问题。

我详细介绍了我的完整经验和解决方案的更多细节on this page .

关于python - matplotlib 运行但不生成图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37036990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com