1
votes

I want to create a simple histogram in gnuplot and want to adapt the color of the bars according to the data. Currently, I am struggling with the mapping between color and data. Let's say I have the following data file:

X, 500.00, 100.00, 1
Y, 600.00, 200.00, 2

I generate the histogram with the following code:

reset
fontsize = 12
set terminal png
set output "file.png"
set style fill solid 1.00 border 0
set style histogram errorbars gap 2 lw 1
set style data histogram
set xtics rotate by -45
set grid ytics
set xlabel "label"
set ylabel "label"
set yrange [0:*]
set datafile separator ","
plot 'data.dat' using 2:3:4:xtic(1) ti "" lc variable

Now I want to create a mapping between the fourth column in the data and the color, e.g. 1 -> yellow, 2 -> blue. I assumed that I can define something like the following

set style line 1 linecolor rgb "yellow"
set style line 2 linecolor rgb "blue"

but this code is not working since it defines styles and not colors. On the other hand I have read in the documentation that "rgb variable" is only available in 3D plotting mode (splot), so I think in this terms my whole approach might go in a wrong direction.

Does anyone know how to realise the mapping between data and line colors?

1

1 Answers

0
votes

Have you tried with the command palette? I had the same problems some times ago. I wanted to make this plot (that is in some ways what you need) My histogram So I used the number of elements in a column of the histogram to set the color of that column. My datafile looked like

#MY_FILE
...
26 0.02302 2302
28 0.02233 2233
30 0.02261 2261
32 0.02383 2383
34 0.02279 2279
36 0.02366 2366
38 0.02226 2226
40 0.02148 2148
...
#EOF

where the first row $1 was the n parameter, the second one $2 was my pdf (simply the histogram normalized) and the third $3 column was the number of occurrence int the bin. Then I used the last column as the parameter to color my graph with the command

 set palette model RGB defined (1 "blue", 2 "red")

that create a gradient between the starting point 1 and the end 2. Then to use the palette i plotted with the line

p 'MY_FILE' u 1:2:3 w boxes palette

where the w boxes was the command to generate my histogram, and the palette (also pal) command was the command o set the color, which use the third column as specified in u 1:2:3, where the 1:2 is my histogram and 3 is the color gradient. if you don't want the lateral strip of color (heatmap) just type in gnuplot

unset colorbox

Here's some documentation about palette command in gnuplot:

  1. http://gnuplot.sourceforge.net/demo_5.0/pm3dcolors.html
  2. type help palette on gnuplot
  3. THIS could be particularly HELPFUL: http://www.gnuplotting.org/defining-a-palette-with-discrete-colors/