matplotlib pyplot imshow show did not work and show image with virtualenv

origin blog: https://www.pyimagesearch.com/2015/08/24/resolved-matplotlib-figures-not-showing-up-or-displaying/

Problem:Matplotlib figures not showing up or displaying

As I hinted at earlier in this post, the missing figure issue is related to the matplotlib backend that does all the heavy lifting behind the scenes to prepare the figure.

Popping into a shell, I can access the matplotlib backend using the

matplotlib.get_backend()

$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.get_backend()
'agg'

 

On my Ubuntu machine this gives me a value of

agg

 ; however, through my testing and debugging, this value needs to be

TkAgg

  for the TkInter windowing system (at least when using Ubuntu and Raspbian).

Luckily, we can resolve this issue by using

apt-get

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

 

But we’re not quite done yet. In order to get matplotlib to recognize the TkInter GUI library, we need to:

  • Step 1: Access our

    plotting

      virtual environment via

    workon plotting

     .
  • Step 2: Use pip to uninstall

    matplotlib

     (since we installed it via pip earlier in this article).
  • Step 3: Pull down matplotlib from the GitHub repo.
  • Step 4: Install

    matplotlib

      from source using

    setup.py

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

     

Again, you’ll want to ensure that you have installed TkInter via

apt-get

  before performing these steps.

After

matplotlib

  has been installed via source, let’s execute the

get_backend() 


$ python

Python 3.4.0 (default, Apr 11 2014, 13:05:11)

[GCC 4.8.2] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import matplotlib

>>> matplotlib.get_backend()

'TkAgg'

Sure enough, we now see the

TkAgg

  is being used as the

matplotlib

  backend.

Note: You can explicitly instruct

matplotlib

  to use the

TkAgg

  backend by making a call to

matplotlib.use("TkAgg")

 ; however, this won’t do you much good if the TkInter dependencies are not installed.

And now when we execute our

grayscale_histogram.py

 

 

 

Figure 4: Success! Our matplotlib figure is now showing up! All we need to do was change the matplotlib backend.

We have now fixed our issue — matplotlib figures are successfully being displayed on our screen!

Granted, this solution is a bit of a pain in the ass, but it’s fairly straightforward and gets the job done. If you have any other suggestions or comments, please feel free to leave them in the comments section.

What about the Raspberry Pi?

The Raspbian operating system, which many Raspberry Pi’s run, is Debian based just like Ubuntu. If you are having the same problems with matplotlib figures not displaying on your Raspberry Pi, the fix detailed in this blog post will resolve your plotting woes.

Can’t you just install matplotlib via apt-get?

The astute Debian user may be wondering why I didn’t simply install

matplotlib

  via

apt-get

 

Resolved: Matplotlib figures not showing up or displaying

$ sudo apt-get install python-matplotlib

The reason is because I’m a heavy user of Python virtual environments and strictly believe in keeping my Python environments sequestered and independent of each other. If you use

apt-get

  to install

matplotlib

  you lose control over what version of

matplotlib

  you want to install — you simply have to use with whatever version is in the

apt-get

  repository. This also muddles your system install of Python which I try to keep as clean as possible.

All that said, every time I have installed

matplotlib

  via

apt-get

  all of my dependencies were correctly installed and I was able to display my figures without a problem, so if you do not care about Python virtual environments, then the

apt-get

  solution is a good way to go. But again, I really recommend using virtual environments.

Summary

In this blog post I detailed how to resolve a pesky issue where

matplotlib

  figures are not displayed to your screen. Symptoms of this problem include clean script execution (i.e. no error messages and no warnings) printed to your terminal, and yet your plot is not displayed. I have regularly encountered this problem when using Debian based operating systems such as Ubuntu and Raspbian. The problem is only further compounded when using Python virtual environments.

Resolving this

matplotlib

  issue involves manually installing dependencies via

apt-get

  and adjusting the matplotlib backend to use

TkAgg

 , followed by compiling and installing

matplotlib

  from source. Afterwards, the issue seems to be resolved.

While this post wasn’t related to computer vision, the

matplotlib

  library is heavily used in the scientific Python community, so not having your

matplotlib

  figures displayed can be extremely frustrating and annoying. I hope this post helps other readers who encounter a similar problem.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值