Friday, December 7, 2007

Nicer Fonts for Gnuplot

Gnuplot is a great visualization tool to use. However the default font is not so pleasant for me. Following is the record on my adventure for alternative fonts. The description is mainly Gentoo oriented.

Which font to use?

I'd like to try TTF font for nicer looking, and Bitstream Vera bundled with Gnome just fits my taste. In case that you have not installed Gnome (e.g. you're pretty much satisfied with the plain terminal, or you use X only without any desktop environment, or you are a fan of other desktop environments like KDE or E17), you may install the font with one command in Gentoo: emerge ttf-bitstream-vera.

Build Gnuplot

Gnuplot should be built with libgd support. To do so, you should enable the USE flag "gd". You may add gd to your make.conf, or use the following command to add it for gnuplot alone:

echo "sci-visualization/gnuplot gd" >> /etc/portage/package.use

After that, build gnuplot by typing emerge gnuplot. The version I'm using for this post is 4.2.2.

Configure environment variable

Add the following line to one of the shell profiles (e.g. ~/.bash_profile):

export GDFONTPATH=/usr/share/fonts/ttf-bitstream-vera

Try it out

Now launch gnuplot. To test whether our settings take effect, type the following at gnuplot prompt:

gnuplot> set term png enhanced font "Vera,12"
Terminal type set to 'png'
Options are 'nocrop enhanced font Vera 12 '
gnuplot> set output "test.png"
gnuplot> plot sin(x)
gnuplot> quit

Now start your favorite photo viewer to examine test.png (I use Emacs in this case). Does the figure looks prettier than before? (Note: it seems that the font name is case sensitive and should be consistent with the actual file name of the font: "Vera" works in above example while "vera" not.)

One limitation is that TTF fonts are not available to X11 terminal therefore you cannot directly enjoy the effect within gnuplot. Emacs users using gnuplot-mode might not feel too much switching pain in this case. Another solution is: if you have ImageMagick installed, you can use png terminal, and pipe the output to ImageMagick by typing set output '|display png:-' instead of plain test.png as shown above.

4 comments:

  1. gnuplot> set term png enhanced font "Vera,12"

    doesn't work for me, but

    gnuplot> set term png enhanced font Vera 12

    does.

    Also, one can give the complete .ttf filename: In my case:

    set term png enhanced font '/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf' 14

    ReplyDelete
  2. @anonymous: thanks for your comments. I assume this is a version problem: I'm using v4.2.2 as stated in the post. All the 3 methods discussed in your comment work for me (note: on my Gentoo box, Vera.ttf is located in "/usr/share/fonts/ttf-bitstream-vera").

    ReplyDelete
  3. Thanks, your post was a big help for me! I am getting fed up with all the hassle in gnuplot, though. I think I'll start looking at Tioga which is supposed to be really good.

    ReplyDelete
  4. Thanks for the info. Very helpful when using gnuplot

    ReplyDelete