Friday, October 21, 2011

Colorbar titles in matplotlib

In matplotlib sometimes you want to put some title in a colorbar, when producing a filled contour plot or similar. One way is to simply:

Another way is to use the set_title or the set_xlabel of your colorbar axes. For the first:

However in this way I don't know how to set the title at a distance from the axes. For the second, this can be done using the labelpad:

Tuesday, October 11, 2011

Avoid possible division by zero by limiting the divisor

The friendly "division by zero" warning has appeared to almost anyone that had a pocket calculator. Although this can be a nice greeting from our little digital friends that is bidding us to give it some more though on our problem and the required computations, it is generally a pain if one has a large code and has to wait several hours, if not days, to finish a computation. It is sometimes natural that in the progress of the numerical steps needed to solve a PDE where the initial and some boundary conditions are dubious, that the solution needs to wander a bit before giving up and returning those anxiously awaited results.

So, for an operation "a/b" where "b" is may be 0 at some point, a limiter can be applied such that a minimum threshold, dictated by parameter "SMALL", forces the value to be higher than zero and also to avoid underflows.

Welcome

I am human, thus I forget things. Well, sometimes these are quite useful things that in the instances when these are really needed, coincidently it is when these are harder to remember. I have used notebooks for a long time but when these are also needed, they are never around, which is a beautiful fact adding to the arguments of Murphy's law. As such, tired of searching for these multiple pieces of code when a difficulty comes by, I've decided to create this blog to put the stuff that I learn, from other people or by trial and error, so that it is available through some simple mouse clicks.

My daily activity concerns programming to solve problems related with numerical things.
The main programming I work with is FORTRAN 90 and the interpreted languages I use are Python (mainly numpy, scipy, matplotlib), Matlab, Maxima. My preferred shell is Bash and my text editor is good old Vim. Regarding text processing, Latex is a must.