I think a better title for this blog post might be: How I lost a day of productivity to Ubuntu, virtual environments, matplotlib, and rendering backends.
Over the weekend I was playing around with deep learning on my Ubuntu system and went to plot the accuracy scores of my classifier. I coded up a quick Python script using matplotlib, executed the script, only to not have the figure displayed to my screen.
My script executed just fine. No error messages. No warnings. But there was still no plot to be found!
This is actually a common problem I’ve ran into over the past few months, especially when working with Debian based operating systems such as Ubuntu and Raspbian. This issue is only further compounded when utilizing virtual environments via the virtualenv and virtualenvwrapper packages.
The issue actually stems from the matplotlib backend not being properly set, or from a missing dependency when compiling and installing matplotlib. Luckily, after a lot of trial and error (and spending an entire day trying to come up with a solution), I have been able to resolve the problem and get matplotlib figures to show up and display on my screen on both the Ubuntu and Raspbian operating systems (and when using Python virtual environments).
While this post is not exactly related to computer vision or OpenCV, I still want to share my experience and solution with other PyImageSearch readers. Matplotlib is a heavily used package in the Python scientific community and I hope that this article helps other readers resolve this strange and hard to pinpoint issue.
Setting the stage
Let’s go ahead and set the stage.
- We’re using a Debian based operating system such as Ubuntu or Raspbian.
- We’re (optionally) utilizing Python virtual environments via virtualenv and virtualenvwrapper.
- And our goal is to take the following image (left) and compute a grayscale pixel intensity histogram for it using matplotlib (right):
Since we are using matplotlib, let’s create a new virtual environment called
$ mkvirtualenv plotting
Now that we’re in the
plotting environment, let’s install
scipy , and
$ pip install numpy $ pip install scipy $ pip install matplotlib
Awesome — all of our Python dependencies are installed. Now, let’s write a few lines of code to load the image, convert it to grayscale, compute a histogram over the grayscale image, and finally display it to our screen. I’ll throw all this code into a file named
# import the necessary packages from matplotlib import pyplot as plt import cv2 # load the image, convert it to grayscale, and show it image = cv2.imread("raptors.jpg") gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Image", image) cv2.imshow("Gray", gray) cv2.waitKey(0) # construct a grayscale histogram hist = cv2.calcHist([gray], , None, , [0, 256]) # plot the histogram plt.figure() plt.title("Grayscale Histogram") plt.xlabel("Bins") plt.ylabel("# of Pixels") plt.plot(hist) plt.xlim([0, 256]) plt.show() cv2.waitKey(0)
The code here is fairly straightforward. Lines 1 and 2 import
cv2 . We then load our image and convert it to grayscale (Lines 4-9). From there the
cv2.calcHist function is used to compute a histogram over the grayscale pixel intensities. Finally, Lines 14-22 plot the histogram using
To execute our script, all we need to do is fire up and shell and issue the following command:
$ python grayscale_histogram.py
When I execute the code on my OSX machine in the
plotting virtual environment, the histogram is computed and both the grayscale image and histogram are displayed to my screen:
However, when I go over to my Ubuntu 14.04 machine and execute the exact same code all I see are my images:
Which leads to the question: “Where is the histogram?”
As we can see from the terminal output, the script executed just fine. No errors were displayed. No warning messages printed to my console. But yet there is not plot!
Resolved: 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
$ 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 to install a few libraries:
$ 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
plottingvirtual environment via
- 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
matplotlibfrom source using
I can accomplish these steps using the following commands:
$ 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.
matplotlib has been installed via source, let’s execute the
get_backend() function again:
$ 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
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 script, just like above:
$ python grayscale_histogram.py
We should now see both our grayscale image along with our histogram:
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
apt-get , like this:
$ 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
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.
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.
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.
I’ll be back next week with more computer vision posts!