user3616643
user3616643

Reputation: 153

Gnuplot bar chart from this CSV input

I have csv file data.dat for example with these values:

#W=1
0;sqlite;11500
1;hsql;14550
2;h2;17550
#W=2
0;sqlite;11000
1;hsql;13800
2;h2;16500
#W=3
0;sqlite;11000
1;hsql;13800
2;h2;16500
#W=4
0;sqlite;11000
1;hsql;13800
2;h2;16500 

I need to plot bar charts into pdf. each data for each graph starts with title #W1,#W2....

i have tried this script:

 cat << __EOF | gnuplot -persist
 set terminal pdf
 set output 'out.pdf'
 set datafile separator ";"
 set boxwidth 0.5
 set style fill solid
 plot "data2.dat" using 1:3:xtic(2) with boxes
__EOF

giving this output: enter image description here

how can i make more graphs (4 in this example but there could more or less) from one datafile each graph separately under previous one?

Edit: each graph can have different number of bars not only three as in previous example. File data2.dat could also look like this:

#W=1
0;sqlite;11500
1;hsql;14550
2;h2;17550
3;derby;8500
4;sqlite;13550
5;h2;19250
#W=2
0;sqlite;11000
1;hsql;13800

This script from Miguel answer works fine when there are 3 bars for each graph but i dont know how to change it to plot more or less bars.

cat << __EOF | gnuplot -persist
set terminal pdf
set datafile separator ";"
set boxwidth 0.5
set style fill solid
do for [i=0:3] {
set output 'out'.i.'.pdf'
plot "data2.dat" using 1:3:xtic(2) every ::(i*3)::(i*3+3) with boxes
}
__EOF

also if it would be possible i would like to have a graph tittle according to header value of each dataset (W=1, W=2....)

Upvotes: 1

Views: 2750

Answers (1)

Miguel
Miguel

Reputation: 7627

You can use multiplot and an outer loop of your plot command, or you can iterate within you plot command, depending on what you want to achieve. The every option allows you to select what to plot from your file.

With multiplot:

set multiplot layout 2,2
set datafile separator ";"
set boxwidth 0.5
set style fill solid
do for [i=0:3] {
plot "data2.dat" using 1:3:xtic(2) every ::(i*3)::(i*3+3) with boxes
}

enter image description here

Within a single plot:

set datafile separator ";"
set boxwidth 0.5
set style fill solid
plot for [i=0:3] "data2.dat" using 1:3:xtic(2) every ::(i*3)::(i*3+3) with boxes

enter image description here

In your case, since the last 3 blocks in your file are the same you don't get to visualize above the 2nd and 3rd blocks, which remain hidden under the 4th one.

To have the graphs plotted in independent files again you just need to loop, but now changing the file name for each iteration:

set terminal pdf
set datafile separator ";"
set boxwidth 0.5
set style fill solid
do for [i=0:3] {
set output 'out'.i.'.pdf'
plot "data2.dat" using 1:3:xtic(2) every ::(i*3)::(i*3+3) with boxes
}

The code above will generate files out0.pdf, out1.pdf, etc.

Upvotes: 1

Related Questions