How to remove padding around plot in Matplotlib

The plot generated by Matplotlib typically has a lot of padding around it. This is useful if you are viewing or displaying the plot in isolation. However, when the plot is embedded inside another document, typically extra padding is added around and makes the plot look tiny. The solution is to reduce or remove the padding around the plot generated by Matplotlib.

This can be done by configuring the bounding box used for the plot while saving it to disk:

import matplotlib.pyplot as mplot
mplot.savefig("foo.pdf", bbox_inches="tight")

This makes the bounding box tight around the plot, while still giving enough space for the text or lines on the plot periphery.

If you want a plot with zero padding around it:

import matplotlib.pyplot as mplot
mplot.savefig("foo.pdf", bbox_inches="tight", pad_inches=0)

Personally, I find this too tight, but it might be useful in some situations.

Tried with: Matplotlib 1.4.3, Python 2.7.3 and Ubuntu 14.04

How to color by bar height in 3D bar plot of Matlab

3D bar plots can be created easily in Matlab using the bar3 function. The bar columns in the plot are colored based on the row and column they belong to. It might sometimes be more useful to see the color of the bar column be based on the value (or z or height) of that bar column.

One way to do this is to use the bar3c function written by Gunther Struyf. Download his bar3c.m file from here, place it in the Matlab directory you are working from and call the function passing it the data.

Tried with: Matlab (R2013a)

How to generate XKCD plot using Matplotlib

Generate XKCD plot like this by adding a single function call!
Generate XKCD plot like this by adding a single function call!

Don’t you think those plots, charts and graphs used in XKCD comic strips look really cool? I also think that by mimicking handwriting, these plots actually make the data more understandable. But, that’s just me.

Matplotlib now includes support for XKCD plots! And it is really easy to use. If you are already using Matplotlib to generate plots for your data, then you can generate XKCD plots for those easily:

  1. Check if the version of Matplotlib on your computer supports XKCD plots. You can do this with the Python interpreter:
$ python
>>> import matplotlib.pyplot
>>> matplotlib.pyplot.xkcd()
<matplotlib.rc_context object at 0x1ff9950>

If you see the above object being created then your Matplotlib supports XKCD plots. Jump to step 4. If you get a AttributeError: 'module' object has no attribute 'xkcd' error, then you need to build the latest version of Matplotlib from source. See the next two steps.

  1. Before I built Matplotlib, I needed to install the following packages:
$ sudo apt-get install tcl-dev tk-dev
$ sudo pip install --upgrade distribute

I did these because building Matplotlib needs TCL and TK libraries for displaying plots and it needs a recent version of distribute.

  1. Uninstall your existing Matplotlib and use pip to build and install the latest version for you:
$ sudo apt-get remove python-matplotlib
$ sudo pip install matplotlib

Hopefully, that installation should work. If it fails during building then check what libraries its missing and install those packages.

  1. XKCD plots use a font named Humor Sans. Download it from here. To install it follow the steps described here.

  2. You are ready to generate XKCD plots. Just add the call matplotlib.pyplot.xkcd() before the other plotting calls in plot generating Python code. The generated plots will now be like in XKCD! πŸ™‚

Tried with: Matplotlib 1.3.0 and Ubuntu 12.04 LTS