My Python setup

In this post, I summarise my custom settings for my IPython, Matplotlib and IPython Notebook for my Windows machine at work.


First some aesthetic preferences:

# select qt backend for plotting from terminal
from IPython.terminal.ipapp import TerminalIPythonApp

c = get_config()
# bigger plots in IPython notebooks
c.InlineBackend.rc = {'figure.figsize': (16.0, 8.0), 'savefig.dpi': 72}

These are for productivity, mainly:

# always reload packages interactively
%load_ext autoreload
%autoreload 2

# import mainly used packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# change to pandas default "beautiful" style
pd.options.display.mpl_style = 'default'
# but keep sans-serif fonts (instead of monospace)
plt.rcParams[''] = 'sans-serif'


For coherence with my employer’s style guide, I set my plot font to Arial with the following two lines:         : sans-serif
font.sans-serif     : Arial

All this results in the following default plot appearance (here for a Pandas area plot of multiple timeseries):


Share as Gist toolbar button

Pre-requisite: you must have a GitHub account and create a Personal access token for your share button, so that it may upload code in your name.

Once that is prepared, get the file gist.js from minrk’s ipython extensions GitHub repository and place it in in the folder .ipython/nbextensions.

Then add the following lines to file custom.js, located in your profile:

require(["nbextensions/gist"], function (gist_extension) {
    console.log('gist extension loaded');

The next time you launch ipython notebook, a share button should appear in every notebook’s toolbar. The first time you click it, you must paste your GitHub access token. From there on, it will work automatically. Here is how it looks in action:


(And here is the link to the corresponding Notebook: urbs-result-plot.ipynb.)

How to change the default matplotlib font

The default fonts for matplotlib are set in file matplotlibrc in the folder $USER_HOME/.matplotlib. You can find them here, depending on your operating system:

  • Windows: C:\Users\YourName\.matplotlib\matplotlibrc
  • Linux: /home/YourName/.matplotlib/matplotlibrc

Within the file, search for the font section, and uncomment (remove the hash symbol # at beginning of line) or add the following settings as desired. In the shown example, I set Arial as the default:         : sans-serif          : normal
#font.variant        : normal
#font.weight         : medium
#font.stretch        : normal

# ...

#font.serif          : Times New Roman, ...
font.sans-serif     : Arial, ...
#font.cursive        : ... cursive
#font.fantasy        : ... fantasy
#font.monospace      : ... monospace

Upon the next start of the Python interpreter, your plots should be labelled with the new font.

Further reading

How to autoreload packages while using them in IPython

I use IPython to interactively use and debug code that I edit in a text editor at the same time. Unfortunately, Python does not automatically reload packages and functions after an initial import foo, simply for performance reasons.

Fortunately, there is a solution: the IPython extension autoreload does what its name says: either all (how I use it) or only selected (imported using magic function %aimport) are refreshed, whenever I hit the Enter key. Here’s a short how-i-use-it demo, involving a minimal package foo with a helloworld function bar:

In [1]: %load_ext autoreload
In [2]: %autoreload 2
In [3]: import foo
In [4]:
Hello World!

In [5]: !cat
def bar():
    print('Hello World!\n')

In [6]: # edit in editor
In [7]: !cat
def bar():
    print('Hello me!\n')

In [8]: # bar() is automagically reloaded
In [9]:
Hello me!

How do you get it? It’s already bundled with IPython by default! Happy hacking!