Installing matplotlib on Windows via cygwin

Use cygwin’s package manager to install python 2.7

Make sure you have matching version of gcc and g++ installed.  For example, my gcc is version 4, but my g++ was v3, so I installed g++ v4.  If they’re out of sync, you may run into this error:

 gcc -fno-strict-aliasing -ggdb -O2 -pipe -fdebug-prefix-map=/home/jt/rel/python-2.7.3-1/python-2.7.3-1/build=/usr/src/debug/python-2.7.3-1 -fdebug-prefix-map=/home/jt/rel/python-2.7.3-1/python-2.7.3-1/src/Python-2.7.3=/usr/src/debug/python-2.7.3-1 -DNDEBUG -g -fwrapv -O3 -Wall -DPY_ARRAY_UNIQUE_SYMBOL=MPL_ARRAY_API -DPYCXX_ISO_CPP_LIB=1 -I/usr/local/include -I/usr/include -I/usr/lib/python2.7/site-packages/numpy/core/include -I/usr/include -I. -I/usr/local/include/freetype2 -I/usr/include/freetype2 -I/usr/lib/python2.7/site-packages/numpy/core/include/freetype2 -I/usr/include/freetype2 -I./freetype2 -I/usr/include/python2.7 -c src/ft2font.cpp -o build/temp.cygwin-1.7.17-i686-2.7/src/ft2font.o
 gcc: error trying to exec 'cc1plus': execvp: No such file or directory
 error: command 'gcc' failed with exit status 1

Other Cygwin packages that appear to be prerequisites:

pkg-config, freetype2, libfreetype-devel, libpng-devel, gtk2.0, libgtk2.0-devel

Once they’re installed, run: “easy_install pip”, and then “pip install matplotlib”

The installation process will put a log of its experience in:

~/.pip/pip.log

By default, matplotlib wants to be connected to a display, so you either need a Cygwin X-terminal, or you can invoke matplotlib with a non-interactive backend as follows:

import matplotlib
matplotlib.use('pdf')  # Use “Agg” for png (can use multiple .use() invocations to use more than one)
import matplotlib.pyplot as plt

It’s important to do the .use() before importing anything else from matplotlib.
I then ran into the following font error:

Traceback (most recent call last):
File "./parseData.py", line 1449, in <module>
main()
File "./parseData.py", line 1432, in main
posterGraph(args)
File "./parseData.py", line 1269, in posterGraph
compare.genPosterGraph(N, timing, figDir=figsDir)
File "/cygdrive/d/home/svn/elgreco/qsp-practical/code/perf/compare.py", line 238, in genPosterGraph
fig.savefig(name + ".pdf",bbox_inches='tight',dpi=dpi)
File "/usr/lib/python2.7/site-packages/matplotlib/figure.py", line 1363, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 2052, in print_figure**kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 2301, in print_pdf
self.figure.draw(renderer)
File "/usr/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/figure.py", line 999, in draw
func(*args)
File "/usr/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "/usr/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/axis.py", line 1053, in draw
tick.draw(renderer)
File "/usr/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/axis.py", line 240, in draw
self.label1.draw(renderer)
File "/usr/lib/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/matplotlib/text.py", line 596, in draw
ismath=ismath)
File "/usr/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 1792, in draw_text
self.track_characters(font, s)
File "/usr/lib/python2.7/site-packages/matplotlib/backends/backend_pdf.py", line 1465, in track_characters
realpath, stat_key = get_realpath_and_stat(fname)
File "/usr/lib/python2.7/site-packages/matplotlib/cbook.py", line 788, in __call__
stat = os.stat(realpath)
OSError: [Errno 2] No such file or directory: '/cygdrive/d/home/svn/elgreco/qsp-practical/code/perf/C:\\Python27\\lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\Vera.ttf'

which appears to arise due to a bug in the cbook.py script.  I edited line 785 to add set realpath to path, since path was being set correctly to the C:\\Python27\\…  but the earlier call to realpath = os.path.realpath(path) was causing the weird concatenation shown above.  Thus, I ended up with:

class GetRealpathAndStat:
   def __init__(self):
     self._cache = {}
   def __call__(self, path):
     result = self._cache.get(path)
     if result is None:
         realpath = os.path.realpath(path)
         if sys.platform == 'win32':
            stat_key = realpath
         else:
            realpath = path
            stat = os.stat(realpath)
            stat_key = (stat.st_ino, stat.st_dev)
         result = realpath, stat_key
         self._cache[path] = result
      return result
get_realpath_and_stat = GetRealpathAndStat()

Leave a Reply