Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to map a string to a RGB value?

The data file is like the following:

# ID,Value, Region
A,30,North
B,26,North
C,49,South
D,55,East
...

Here I would like to map the string (e.g., Region) to a rgb value which will be used as the lc color for boxxy. It can be described as the pseudo-code:

if (region eq "North") {
   "0x1b9e77"
} else if (region eq "South") {
   "0xd95f02"
} else if (region eq "East") {
   "0x7570b3"
} else  {
   "0xe7298a"
}

How can I achieve such mapping in gnuplot? Here, in fact, it is to map $3 to a hex string in using option.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can define a mapping function basing on your pseudocode:

colormapper(x)=(x eq "North"?0x1b9e77:(x eq "South"?0xd95f02:(x eq"East"?0x7570b3:0xe7298a)))

after that, you can apply it on your column.

plot 'map.dat' u 1:2:(colormapper(stringcolumn(3))) pt 7 ps 3 lc rgb variable notitle

Sample based on my data

The key moments of the solution:

  • Using x eq "North"... because we compare strings
  • lc rgb variable let us to control the line color by a variable (without rgb you can access to the standard line colors)
  • Using stringcolumn(3) instead of $3 is very important.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading