Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

label, awk/grep, histogram

118 views
Skip to first unread message

bryan

unread,
Jun 29, 2010, 1:56:49 PM6/29/10
to
i want to put labels above the bars+/- error in a histogram using awk/
grep within gnuplot to calculate the position of the label from the
data itself e.g. move it up by 0.1 from the data point. my gnuplot
script and data are below - tried to keep it simple. as is, the
labels show up on 0,0 (i think) instead of above the top of the bar.
the grep/awk bit seems to work as tested with !, but still i can't see
why this is not working. i tried printf also, that did NOT work.
there are no error messages.

-bryan

# gnuplot script :
reset ; set border 9 ; set ytics nomirror ; \
set xrange [0:5] ; set yrange [0:0.5] ; set y2range [0:0.5]; \
set y2tics 0.1 nomirror rotate by 90 ; unset ytics ; \
set style histogram clustered gap 1 ; set y2label "Juiciness" ; \
set label "Label 1" at "< grep Banana data_1.dat | awk '{print $1
"," ($2+0.1)}'" textcolor lt -1 rotate by 90 ; \
set label "Label 2" at "< grep Apple data_1.dat | awk '{print $1
"," ($2+0.1)}'" textcolor lt -1 rotate by 90 ; \
set label "Label 3" at "< grep Kiwi data_1.dat | awk '{print $1
"," ($2+0.1)}'" textcolor lt -1 rotate by 90 ; \
set label "Label 4" at "< grep Orange data_1.dat | awk '{print $1
"," ($2+0.1)}'" textcolor lt -1 rotate by 90 ; \
set style fill solid border -1 ; set xtics offset 0,-2.5 nomirror
rotate by 90 ;
plot \
for [i=0:1] 'data_1.dat' u 1:2:3:xticlabel(4) every ::i w boxerror
notitle lw 2 lc 1, \
for [i=2:3] 'data_1.dat' u 1:2:3:xticlabel(4) every ::i w boxerror
notitle lw 2 lc 1

# data :

1 0.2938 0.0664 "Banana"
2 0.1184 0.0346 "Apple"
3 0.1938 0.0464 "Kiwi"
4 0.2184 0.0546 "Orange"

Péter Juhász

unread,
Jun 29, 2010, 6:44:48 PM6/29/10
to

Use the 'labels' style (help labels). For example, amend your plot
command with

for [i=1:4] '' u 1:($2+0.1):("Label ".i) every ::i w labels

Péter Juhász

bryan

unread,
Jun 29, 2010, 7:46:45 PM6/29/10
to
> for [i=1:4] '' u 1:($2+0.1):("Label ".i) every ::i w labels

nice! i did this :

for [i=0:3] '' u 1:($2+0.1):5:xticlabel(5) every ::i w labels notitle
lw 2 lc -1

... it doesn't look like the labels can be rotated that way - is that
right?

-bryan

Péter Juhász

unread,
Jun 30, 2010, 3:11:52 AM6/30/10
to

They can be rotated alright! Use the 'rotate' keyword, just as you
would with "set label".
The order of keywords is not completely free, certain combinations may
give an error message.
Look up the syntax of "set label", the order described there is
guaranteed to work.

By the way, 'lw 2 lc -1' does not do what you think it does. 'lw 2' is
meaningless, use 'font ",fontsize"', where fontsize is a number, if
you want to control font size. And write 'tc lt -1' instead of 'lc -1'
- perhaps their full form 'textcolor' instead of 'linecolor' is more
intuitive.

So, putting it together:

plot for [i=0:3] '' u 1:($2+0.1):5:xticlabel(5) every ::i w labels
rotate by 90 tc lt -1 notitle

A tip to set font sizes: declare a variable, like 'fs = 10'. Use that
when you set up your terminal:

set term whatever font "fontname,".fs

Then use

font ",".(fs*2)

in your plot command to get labels that are twice as big as the tick
markings, axis labels etc. This way, if you change your terminal, or
your plot size, you can just set fs to the desired value, and the
labels created by 'plot' will scale accordingly.

Péter Juhász

bryan

unread,
Jun 30, 2010, 2:47:53 PM6/30/10
to
On Jun 30, 3:11 am, Péter Juhász <peter.juhas...@gmail.com> wrote:
> So, putting it together:
>
> plot for [i=0:3] '' u 1:($2+0.1):5:xticlabel(5) every ::i w labels rotate by 90 tc lt -1 notitle

this would be brillant except that all the xticlabels are lost (see
new data file) - is this an impossible 'conflict'?:

for [i=0:1] 'data_1.dat' u 1:2:3:xticlabel(4) every ::i w boxerror
notitle lw 2 lc 1,

for [i=2:2] '' u 1:2 every ::i notitle lc -1 ps 0 lw 0,
for [i=3:4] '' u 1:2:3:xticlabel(4) every ::i w boxerror notitle lw 2
lc 2,
for [i=0:4] '' u 1:($2+0.1):5:xticlabel(5) every ::i w labels notitle
lw 2 tc lt -1 rotate by 90


new data file :
1 0.2938 0.0664 "Banana" ++
2 0.1184 0.0346 "Apple" +/-
3 0 0 "" .
4 0.1938 0.0464 "Kiwi" -
5 0.2184 0.0546 "Orange" +++


bryan

unread,
Jun 30, 2010, 7:39:17 PM6/30/10
to
thank you Peter, sfeam - my script is below with everything I want
(see also previous post :
http://groups.google.com/group/comp.graphics.apps.gnuplot/browse_thread/thread/80e72a1596140909/11d1b57cb5386134#11d1b57cb5386134)


reset ; set border 9 ; set ytics nomirror ; set xtics scale 0
nomirror ;
max=0.5 ; fs=18 ; unset ytics ; set boxwidth 0.5 ;
set xrange [0:6] ; set yrange [0:max] ; set y2range [0:max];
set y2tics 0.1 nomirror rotate by 90 ; set bars back ;
set style histogram clustered gap 1 title offset 2,0.25 ; set y2label
"Juiciness" ;


set style fill solid border -1 ; set xtics offset 0,-2.5 nomirror
rotate by 90 ;
plot

for [i=0:4] 'data_1.dat' u 1:($2+0.1):5:xticlabel(5) every ::i w
labels notitle lw 2 tc lt -1 rotate by 90,
for [i=0:1] '' u 1:2:3:xticlabel(4) every ::i w boxerror fill empty
notitle lw 2 lc -1,
for [i=3:4] '' u 1:2:3:xticlabel(4) every ::i w boxerror fill empty
notitle lw 2 lc -1,
for [i=0:1] '' u 1:2 every ::i w boxes notitle lc 1,


for [i=2:2] '' u 1:2 every ::i notitle lc -1 ps 0 lw 0,

for [i=3:4] '' u 1:2 every ::i w boxes notitle lc 2


data_1.dat :


1 0.2938 0.0664 "Banana" ++
2 0.1184 0.0346 "Apple" +/-
3 0 0 ""

4 0.1938 0.0464 "Kiwi" -
5 0.2184 0.0546 "Orange" +++

... still working on the font tip

-bryan

Péter Juhász

unread,
Jul 1, 2010, 6:31:35 AM7/1/10
to
On Jul 1, 1:39 am, bryan <bryanlep...@gmail.com> wrote:
> thank you Peter, sfeam - my script is below with everything I want
> (see also previous post :http://groups.google.com/group/comp.graphics.apps.gnuplot/browse_thre...)

Cleaned up version:


reset ; set border 9 ;
set ytics nomirror ; set xtics scale 0 nomirror ;

max=0.5 ; fs=18 ; factor = 1

# include these for your final, "production" version
# and then change factor
#set term whatever font ",".fs
#set output "whatever"

unset ytics ; set boxwidth 0.5 ;
set xrange [0:6] ; set yrange [0:max] ; set y2range [0:max];
set y2tics 0.1 nomirror rotate by 90 ; set bars back ;
set style histogram clustered gap 1 title offset 2,0.25 ;
set y2label "Juiciness" ;
set style fill solid border -1 ;
set xtics offset 0,-2.5 nomirror rotate by 90 ;

unset key

plot \
'data_1.dat' u 1:($2+0.1):5 w labels tc lt -1 \
rotate by 90 font sprintf(",%f",fs*factor),\
'' u 1:2:2:($2+$3):xticlabel(4) every ::0::1 w yerror lw 2 lc -1 ps
0,\
'' u 1:2:2:($2+$3):xticlabel(4) every ::3::4 w yerror lw 2 lc -1 ps
0,\
'' u 1:2:($1>2?2:1) w boxes lc var lw 2

0 new messages