2
votes

I have a simple gnuplot script to plot a histogram with differently colored bars, with each color representing a group.

How can I show 3 different keys (1 red, 1 green and 1 blue)?

This is my script:

unset title 
set key left
set yrange [0:10]
set ylabel 'Score'
set xtics rotate out
set style histogram gap 1
set style data histogram
set style fill solid 1.00 border 0
set linetype 1 lc rgb 'red'
set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'red'
set linetype 4 lc rgb 'green'
set linetype 5 lc rgb 'green' 
set linetype 6 lc rgb 'green'
set linetype 7 lc rgb 'blue'
set linetype 8 lc rgb 'blue'
set linetype 9 lc rgb 'blue'
set xtics nomirror 
set ytics nomirror
plot 'example.dat' using ($0):2:($0+1):xtic(1) with boxes linecolor variable notitle

and here my example.dat file:

A 1
B 2
C 3
D 4
E 5
F 6
G 7
H 8
I 9

I have not 10 rep points to post imgs, so these are imgur links to: what I get and what I want

Thanks in advance

1

1 Answers

2
votes

Forget about defining all those styles by hand and work within a loop instead:

unset title 
set key left
set yrange [0:10]
set ylabel 'Score'
set xtics rotate out
set style histogram gap 1
set style data histogram
set style fill solid 1.00 border 0
set xtics nomirror 
set ytics nomirror
plot for [i=1:3] 'example.dat' \
every ::((i-1)*3)::((i-1)*3+2) using ($0+i*3):2:xtic(1) \
with boxes linecolor i title "Gpr".i

enter image description here

The code above loops from 1 to 3, each time plotting one of the groups. every selects the points to plot, and the title is obtained as a string concatenation. The line color is chosen simply as i from 1 to 3, but you could use a complicated conditional expression:

f(x)=(x == 1 ? "magenta" : x == 2 ? "yellow" : "cyan")
plot for [i=1:3] 'example.dat' \
every ::((i-1)*3)::((i-1)*3+2) using ($0+i*3):2:xtic(1) \
with boxes linecolor rgb f(i) title "Gpr".i

enter image description here