Fedex
Fedex

Reputation: 21

Making Histograms with GNUPlot

I want to make two comparative histograms out of one data file, using columns 1,2 and 1,3.

(Data file name is 'Cumulos por Edades.tab')

6.25   46   60
6.75   29   65
7.25   79   70
7.75   87   75
8.25   80   80
8.75   41   84
9.25   28   89
9.75   13   94

I have been able to make something out, but is not what i want. Here is the code.

(Sorry I can't upload an image of the result)

set terminal postscript enhanced color dashed lw 2 "Helvetica" 14
set output "Histograma.ps"
set key horizontal below height 1
set key box lt 1 lc -1 lw 2
set xlabel "log(t)"
set ylabel "N(t)"
set xrange [6:10]
set xtics 6,0.5,10
set grid ytics ls 2 lc rgb "black"
set title "Histograma de Cumulos Abiertos por Edades"
set style data histogram
set style histogram cluster gap 1
plot "Cumulos por Edades.tab" u 1:2 w boxes lc rgb 'blue' t 'Generacion Real',\
"Cumulos por Edades.tab" u 1:3 w boxes lc rgb 'red' t 'Generacion Constante'

What I want to get is an histogram where I can see both columns, but all I can get is the superposition of one over the other.

I would really appreciate if any of you could give me a hand with this.

Upvotes: 2

Views: 6076

Answers (1)

Christoph
Christoph

Reputation: 48430

Using set style data histogram is equivalent to using with histogram. You overwrite this with with boxes.

The easiest way is to plot as histogram. Here is a minimal, running script:

set style data histogram
set style histogram cluster gap 1
plot "Cumulos por Edades.tab" u 2, "" u 3

That shows, how histograms are plotted: The values in the first row are centered at x=0, the second at x=1 and so on. Unfortunately, you can't use the first column as in any other plotting style as numeric x-value. But you could use the value 'as-is' using xtic(1), which reads the value of the first column as string and uses it as tic label:

set style data histogram
set style histogram cluster gap 1
plot "Cumulos por Edades.tab" u 2:xtic(1), "" u 3

The second option is to use the boxes plotting style:

plot "Cumulos por Edades.tab" u 1:2 with boxes, "" u 1:3 with boxes

Here, both columns are plotted around the same x-value, and though overlap. So you need to shift one column a bit to the left and the other to the right. And you must set a fixed boxwidth:

set boxwidth 0.2 absolute
plot "Cumulos por Edades.tab" u ($1-0.1):2 with boxes,\
     "" u ($1+0.1):3 with boxes

Upvotes: 4

Related Questions