#361 Allow palette maxcolors to select non-equally spaced "discrete" contours


If I have the following text file:

1 4  #green
2 11 #yellow
3 40 #red

It would be nice to be able to do something like:

set palette defined (0 "green", 10 "yellow", 20 "red")
set palette maxcolors 3
plot 'test.dat' using 1:2:2 with boxes palette

At the present (tested on gnuplot4.6.0, OS-X 10.5.8), this doesn't work because gnuplot creates a palette as defined above and then partitions it into 3 separate sections (0-15 is green, 15-30 is yellow, 30-45 is red) which isn't what I asked for in my palette definition. Of course there are numerous workarounds, but they all involve re-mapping the 2nd column using a ternary operator in the using spec (which gets tedious very quickly as the number of colors grows), or adding color information to the datafile and doing some sort of loop:

plot for [color in "green red yellow"] 'test.dat' using 1:(strcol(3) eq color ? $2:NaN):(0.95) with boxes

I'm not saying that palette maxcolors should be overloaded for this purpose -- I just used it as an example as it is the closest thing that I know of that gnuplot currently has to do this task -- but it would be nice if there were some way to set the boundaries for the discrete colors in a palette mapping.

Anyway, thanks for the great work and sorry if this request has been made/turned down before.


  • Matthew Gilson

    Matthew Gilson - 2013-04-04

    This page has an example of the type of plot this would be useful for making:


    (the bottom plot turns yellow for KP > 3 and red for KP > 6 -- solar activity is just quiet today). I can imagine it could be useful for all sorts of scripts that have some metric for monitoring something...

  • Ethan Merritt

    Ethan Merritt - 2013-04-04

    The commands you want are

    set palette defined ( \
         0 "green", 10 "green", \
        10 "yellow", 20 "yellow", \
        20 "red", 30 "red" \
    set cbrange [0:30]
    plot 'test.dat' using 1:2:2 with boxes palette fillstyle solid
    1)  The color ranges should be pinned at both ends.
        This makes them independent of the "maxcolors" setting.
        It also allows non-equal color ranges.
    2)  The cbrange must match the range of the palette definition.
        Out-of-range values in the plot will get the color of the
        top or bottom of the range
    3)  I've learned from previous experience that I seem to be
        very bad at explaining this, and in particular at explaining
        why it works this way.  But the above commands do work.
  • Matthew Gilson

    Matthew Gilson - 2013-04-04

    Thanks for the reply. I can see how pinning at either end would make a difference and your solution is already a marked improvement over mine. The thing that would make it better is to somehow relax the "cbrange must match the palette definition range". e.g., something like:

    set palette defined ( \
         0 "green", 10 "green", \
         10 "yellow", 20 "yellow", \
         20 "red", \
    set magic command to let red colorbars extend to infinity or green colorbars to extend to negative infinity
    plot 'test.dat' using 1:2:2 with boxes palette fillstyle solid

    maybe something like set palette pinned to pin the pallete to values rather than a portion of the dynamic range...

  • Ethan Merritt

    Ethan Merritt - 2013-10-26
    • status: open --> closed-works-for-me
    • Group: -->
  • Ethan Merritt

    Ethan Merritt - 2013-10-26

    The program does this already. No need for a change.



Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks