Plotting in Python

Finally start to use python for plotting.

  • Plotting in notebook

ipython notebook is good and plots can be lined up with scripts. But if I need to run the same script on cluster without Xming, it will crash. Simple way is to add

import matplotlib as mpl

when importing libraries.

  • Seaborn

Seaborn is a great package for color scheme picking. Color schemes are called “palettes” in seaborn. There are many pre-set palettes for you to choose. If you want to use “colorblind”, just simply add

import seaborn as sns
with sns.color_palette("colorblind"):

before the plotting script.

Customizing the palette is easy too:

colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
with sns.xkcd_palette(colors):


flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
with sns.color_palette():



Share interactive plots with others through is a online platform where everyone can share their data and make interactive plots together (like Github). It provides a set of application program interface (API) to link your code (written by Python, Matlab, R, etc.) from your desktop to this online environment. All you need to do is generate plot from your own programming system and then push the plot (and the data) to using a single line command (at least in Matlab). After that this plot can be interactively viewed under your account and the data is open to the public.

For example, in Matlab (of course you need to install the API for Matlab beforehand), the command is

p = fig2plotly(fig);

and the plot can be viewed at here. You can zoom in and out and check the data on the plot.

ipython plotting

1. very important websites, containing large number of examples:

2. to open the notebook:

> ipython notebook

to remotely open the notebook:

3. keep the plots and the code in one page without open and new x-window:

> %matplotlib inline

at the top of the code.

4. workflow of creating a public web page for your code and plots:
> save the code as .ipynp
> paste the content to GitHub Gist:
> click “get public gist” and get a number
> input this number into and click “go”

Matlab plot

Plotting in Matlab is a very big topic. Here I just want to share some plotting functions I’ve been using to produce the maps.

pcolor and imagesc

If we have a 4*4 matrix and want to plot it as a 2d surface, two functions (pcolor and imagesc) can be considered. But these two functions yield different results:

create a matrix:

A = hadamard(4);
A = 
1  1  1  1
1 -1  1 -1
1  1 -1 -1
1 -1 -1  1

The pcolor (A) will cut the last column and row then flip upside down before plot. That’s because the first cell (1,1) is considered as origin point (0,0) at lower left. So the plot looks like this (red is 1, blue is -1):


But the imagesc (A) will give the as-is plot: