Matplotlib in Cygwin 64-bit for Python 3

1 minute read

Related: easy pip install matplotlib without commpling


Matplotlib Cygwin manual compile

This method is NOT normally needed.
Just python3 -m pip install matplotlib normally. Matplotlib no longer supports Python 2.7.

  1. at Windows Command Prompt:
    setup-x86_64 -P pkg-config ghostscript libfreetype-devel libpng-devel libgtk2.0-devel gcc-g++ git openbox python3-numpy python3-pyqt5 python3-devel
    
  2. at Cygwin Terminal:
    python3 -m pip install matplotlib
    

Alternative: Matplotlib compile

You can just use the process above, unless you want to compile Matplotlib on Cygwin from source code for the latest Matplotlib beta.

git clone https://github.com/matplotlib/matplotlib.git

python3 -m pip install -e .

Test

It’s important to actually try to make a figure, not just import matplotlib as some errors only occur when the libraries are actually invoked.

from matplotlib.pyplot import figure, show

ax = figure().gca()
ax.plot(range(5))

This may not open a visible figure unless X11 is started as noted in the section below.

visible figures

To create visible Matplotlib figures in Cygwin, start the X11 system by:

  1. Windows Start button → All App → Cygwin-X → Openbox.
  2. Openbox desktop: Right click → Terminals → Xterm.

Test the Matplotlib / Cygwin-X / Openbox configuration from the Cygwin X11 Openbox Xterm terminal:

from numpy.random import rand
import matplotlib

# matplotlib.use('qt5agg') #must be before other matplotlib commands

from matplotlib.pyplot import figure,show

figure().gca().imshow(rand(100,100))
show()

You can permanently set default Cygwin Matplotlib backend to qt5agg.

Save figures

Even without the X11 desktop, create and save invisible figures to .eps, .png, .svg, etc. by using savefig().

from matplotlib.pyplot import figure,show

fg=figure()
ax=fg.gca()
fg.savefig('test.png')

Leave a comment