2011-01-28

Color control with matplotlib

Sometimes it's important to control the color of lines drawn with matplotlib. Two examples I encountered today were drawing a number of horizontal or vertical lines on the same axes and drawing more lines than the number of unique colors in the default color ring. Here's how you can use a colormap to get the desired number of colors:

import matplotlib.pyplot as plt
cmap = plt.get_cmap ('gist_rainbow')
num_colors = 10
for i in xrange (num_colors):
    plt.axhline (i, color = cmap (1.0 * i / num_colors))

2010-11-01

to title case, at the shell

This filter turns the input into "Title Case".

cat [whatever] \
| sed 's/.*/\L&/; s/[a-z]*/\u&/g'

Courtesy of this post.

2010-08-26

Python string concatenation

Looks like the fastest way to assemble your strings, in general, is by generating lists and then calling '{separator}'.join ({string list}), according to Oliver Crow.

2010-05-12

Boost, Autoconf configure error

After upgrading tu Ubuntu 10.04, I started having a problem using an Autoconf-generated configure script to detect the Boost libraries. Ultimately, the problem turned out to be that the libraries just were not installed anymore! Here's what the configure output looked like:

checking for boostlib >= 1.38.0... yes
checking whether the Boost::Filesystem library is available... yes
configure: error: Could not link against  !
Note the two spaces before the "!". That's where the library name should be, if it were even found. So apparently I had the headers left over somehow, but the library had been uninstalled. Something to keep in mind if you see such an error...

2010-04-19

Extract a single file from a tarball

If you've got a tarball with a particular file that you want to extract to standard output, use a command like the following:

tar zxf0 {the tarball} {path to desired file within tarball}

2010-03-11

Gnuplot Histogram

If you want to bin data in Gnuplot, something like this should work (taken from a mailing list):

bw = 5   # substitute what you want 
bin(x,width)=width*floor(x/width) 
plot 'data' using (bin($1,bw)):(1.0) smooth freq with boxes