Friday, September 2, 2011

Palettes

There are several methods for defining a colour palette in GNUPLOT. Here one of them is presented and explained in a simple way. If you want to go deep into this topic, you will have to look for more extensive references (some of them listed as useful links).

First of all, we need to define the number of colours that our palette will contain using this command:
  • set palette maxcolors <number>

Then, what we have to do is to specify the colour of each part of the palette using the following command:
  • set palette defined (<position1> "<colour1>", <position2> "<colour2>", ...)

Example (A):
  • set palette maxcolors 100
  • set palette defined (0 "red", 99 "blue")
These lines will create a palette of 100 colours from red (position 0) to blue (position 99). Between both positions, GNUPLOT will perform a gradation.

We can specify as much colours as we want, for example (B):
  • set palette maxcolors 1000
  • set palette defined (\
  • 0 "red", \
  • 249 "yellow", \
  • 749 "green", \ 
  • 999 "blue")

As a final example, if we want to ascribe a specific colour to the middle value of the palette (in order, for instance, to separate positive and negative values), we will have to write something like this (C):
  • set palette maxcolors 4001
  • set palette defined (\
  • 0 "red", \
  • 1000 "orange", \
  • 1600 "dark-yellow", \
  • 1999 "yellow", \
  • 2000 "grey", \
  • 2001 "green", \
  • 2400 "dark-green", \
  • 3000 "cyan", \
  • 4000 "blue")
Note that only the value in the middle corresponds to gray. The previous and the following values (with a precision that depends on the number of colours selected) will correspond to yellow and green respectively.

Caution: The palettes presented here work in eps output files. If you work with other types of file, you may have some troubles with the colours.

2 comments:

  1. One useful command is "set palette grey" (or "set palette gray" :P) that creates a palette from black to white.

    Also, "set palette maxcolors " is not necessary (but it can be useful!).
    If only a color is given, gnuplot will create a gradient from black to that color.
    If maxcolors is not specified, gnuplot will make gradients between the given colors.

    To use more complicated colors (or "import" them from other programs) RGB hexadecimal values can be used [for instance, set palette defined (0 "#FFFFFF", 0.5 "#FFE9BF", 1 "#FFE095", 1.5 "#E8C070", 4 "#000000") ].

    Also "show palette colornames" prints the defined colors with their names and RGB values.

    ReplyDelete
  2. Thank you again, ABC. I find the "show palette colornames" command particularly useful.

    ReplyDelete