Scientific Python Notes

1 matplotlib

1.1 Removing parts of the frame

Some journals disallow having the right and top lines outlining a plot. For these situations, you can remove them by using methods associated to the spines field of your axis object.

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

Removing the ticks is done like so:

ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')

1.2 Changing the frame linewidth

Changing the frame linewidth is achieved by the set_linewidth method of an axes spine field.

ax.spines['left'].set_linewidth(0.5)
ax.spines['bottom'].set_linewidth(0.5)

1.3 Remove the legend frame

import matplotlib.pyplot as plt
leg = plt.legend()
leg.draw_frame(False)

1.4 Tight layout (fixing clipped axes titles)

For small figures like those which may go in publications, the axes titles and labels may fall outside of the figure boundary and therefore appear clipped.

To fix this, matplotlib provides a function called tight_layout() which automatically moves a subplot to fix the plot within the figure. It also fixes overlapping axis titles in figures with multiple subplots.

tight_layouts() is provided as a method instance of a Figure object, or through pyplot.

plt.tight_layout()

Sometimes, the default settings of tight_layout still do not fix the clipping. In this case, I can adjust its pad, w_pad, or h_pad arguments to adjust the amount of padding introduced by the method. The numeric amounts are fractions of the font size. pad adds padding to the subplots and the figure border, whereas w_pad and h_pad add padding between individual subplots.


1.5 Changing matplotlib font families and size

The fonts in figures can be changed with a little help with matplotlib's font_manager.

The following snippet changes the font family to Arial and the size to 12pt using the font_manager to find the proper name of the font. (Using font_manager isn't necessary if you know the name already.)

import matplotlib.font_manager as font_manager
from matplotlib import rcParams

fontpath = '/usr/share/fonts/truetype/msttcorefonts/Arial.ttf'
prop = font_manager.FontProperties(fname = fontpath)
rcParams['font.family'] = prop.get_name()
rcParams.update({'font.size' : 12})

1.5.1 Clear the matplotlib font cache

It is very important to clear the matplotlib font list cache after adding new fonts to the system. Otherwise, matplotlib will not be able to find your font.

Many forums suggest that this cache is named fontList.cache and is located at ~/.config/matplotlib/. However, this is not the location where my cache file is stored, nor is it the name of the file.

After adding new fonts, I should run the commands

rm -i ~/.cache/matplotlib/fontList.py3k.cache
rm -ri ~/.cache/matplotlib/tex.cache

2 Anaconda

2.1 Copying Anaconda cloud packages

To copy a package on the Anaconda cloud (for example, filelock) into your own channel, use

anaconda copy --to-owner kmdouglass conda-forge/filelock/2.0.7

3 Jupyter at Startup

I can get a Jupyter Notebook server running from startup from a hidden terminal window in Debian Jessie by adding a desktop file to ~./config/autostart named Jupyter.desktop with the following information:

[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=Jupyter
Comment=Starts the Jupyter Notebook Server
Exec=/home/kmdouglass/anaconda3/bin/jupyter-notebook --no-browser
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false

4 Installing and working with multiple versions of Python

I sometimes run into problems if I install multiple versions of Python on the same system. This is largely because my operating system depends on a specific version of Python and its libraries, and these can be corrupted if other versions are installed. In addition, I often want different packages and versions for different projects.

To avoid these issues, I started building Python from source and installing the binaries in a separate folder. I then use a virtualenv that points to these binaries. This keeps all my Python packages and binaries completely separate from the system-level Python install.


4.1 Building Python and installing binaries to a non-default location

It's easy to install the Python binaries to a custom directory by setting the –prefix and –exec-prefix flags when running the configure script:

./configure --prefix=~/src/Python3.4.2 --exec-prefix=~/src/Python3.4.2

After running this command, make, and make install, the binaries and libraries will be placed in these directories instead.


4.2 Setting the interpreter in a virtualenv

After setting up virtualenv and virtualenv wrapper, I can create a new virtual environment that use these binaries with the -p flag in the mkvirtualenv command:

mkvirtualenv -p ~/Python3.4.2/bin/python3.4 MY_VENV_NAME

4.3 Required system packages for optional Python features

The following Debian system packages (I am using Jessie now) are required to build Python with some of its optional features:

Tkinter
tk-dev and possibly python3-tk
dbm
libgdbm-dev

5 Installing Spyder inside a virtuanlenv

Spyder is a pretty good Python IDE for scientific applications. It mimics Matlab's IDE functionality, which, to be honest, is really great even considering the annoying things about the Matlab language.

Unfortunately, Spyder takes little bit of effort to work with virtualenvs.


5.1 Install SIP and PyQt4 inside the virtualenv

The primary difficulty with installing Spyder in a virtualenv is not Spyder itself, but two of its prerequisites: SIP and PyQt4.

Fortunately, there is a relatively painless way to build SIP and PyQt4 for every virtualenv that you want to use Spyder with.

First, make sure you have the required libraries installed:

sudo apt-get install python2.7-dev libxext-dev qt4-dev-tools build-essential

Next, download the SIP and PyQt4 Linux source tarballs and unzip them into separate directories. Enable your virtualenv using workon MY_VIRTUALENV, enter the SIP directory, and type python config.py. When it's done you should see that SIP will be installed to your virtualenv's directory, not your site directory. Finish the SIP installation with make and sudo make install.

After this, move inside the PyQt4 source directory. Configure the Make file with

python configure-ng.py -q /usr/bin/qmake-qt4

Follow this with another make and sudo make install.


5.2 Install Spyder

Once SIP and PyQt4 are installed, it's really easy to install Spyder:

pip install spyder

Its executable is stored in /venv_directories/my_virtualenv/bin/spyder3 (if you're using Python3). Note that you need to build PyQt4 every time you want to install Spyder inside a different virtualenv.