Pregunta

I am plotting a data file with points whose color is defined through a palette that goes from 0 to 1. The color refers to a value in a column of the file; I assign a palette with different hues of "blue":

set cbrange [0:1]

set palette defined (1 "#B0E2FF", 2 "#87CEFA", 3 "#7EC0EE", 4 "#4F94CD", 5 "#4E78A0", 6 "#104E8B", 7 "#1B3F8B", 8 "#162252")

The problem is, the palette gets linearly spaced in colors. So for my data this isn't good, I'd like more precision in the intermediate range of values (large color bins) and small precision in the extremes (small color bins).

I've found I could use a palette in a log scale:

set log cb

but this of course isn't good for me either, given what I want. I'd need to apply a self-defined function to space values in my palette. Is that possible?

¿Fue útil?

Solución

That is possible, if you have a palette defined in terms of three functions, or if you have a data file which holds the palette values.

Consider, you have a mapping function map(x) (i.e. your self-defined function) which maps from [0:1] -> [0:1] and stretches/shrinks the gray range. Then you could use:

red(x) = ...
green(x) = ...
blue(x) = ...
map(x) = ...
set palette functions red(map(gray)), green(map(gray)), blue(map(gray))

And accordingly for a file:

map(x) = ...
set palette file 'palette.dat' using (map($1)):2:3:4

In your case you can:

  1. Define the palette like you want (e.g. with set palette defined ...)
  2. Write the palette to an external data file. This writes one data set for 'red', one for 'green', one for 'blue' and one for the NTSC gray value.
  3. Rearrange the data file to be useful for set palette file. Here I use the following python script palette.py:

    import numpy as np
    import sys
    
    A = np.loadtxt(sys.stdin, usecols=(0,1))
    np.savetxt(sys.stdout, A.reshape(256,8, order='F')[:,(0,4,5,6,7)], fmt='%.3f')
    
  4. Define an appropriate map(x) function.

Here is the script:

# 1.
set palette defined (1 "#B0E2FF", 2 "#87CEFA", 3 "#7EC0EE", 4 "#4F94CD", 5 "#4E78A0", 6 "#104E8B", 7 "#1B3F8B", 8 "#162252")

# 2.
set terminal push
set terminal unknown
set table 'palette.tmp'
test palette
unset table
set terminal pop

# 3.
system('python palette.py < palette.tmp > palette.dat')

# 4.
map(x) = (-0.5+(2*(x-0.5))**3 + x)/3 + 0.5
set palette file 'palette.dat' using (map($1)):2:3:4

test palette
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top