3
votes

I want to plot data using a rowstacked histogram with the data in the same file as the gnuplot commands. I have not found the same question anywhere else, and have tried lots of ideas with no success.

Sample datafile, which I call example.dat

Name    March   April   May
Bob     5       10      30
Alice   20      5       20
Tony    10      10      15
Cathy   15      15      10

Here is a gnuplot command file which I call eg1.gp:

set title "Test chart for row stacked histogram" 
set style data histograms
set style histogram rowstacked
set boxwidth 0.6
set style fill solid 1.0 border -1
set key invert autotitle columnhead
set yrange [:50]
set ylabel "Number of Referrals"
set ytics 10
plot 'example.dat' using 2 t "March", '' using 3 t "April", '' using 4:xtic(1) t "May"

I am using Linux Manjaro. When I issue the command $: gnuplot -persist eg1.gp I get the expected chart which looks like this: Stacked histogram eg1.png

So far so good. The histogram has 4 vertical bars, each with 3 colours representing the months for each name, just as you would expect.

Next, I combine the two files into a single file, adjusting the command to use the data from the file. Here it is:

set title "Test chart for row stacked histogram" 
set style data histograms
set style histogram rowstacked
set boxwidth 0.6
set style fill solid 1.0 border -1
set key invert autotitle columnhead
set yrange [:50]
set ylabel "Number of Referrals"
set ytics 10

plot "-" using 2 t "March", '' using 3 t "April", '' using 4:xtic(1) t "May"
Name    March   April   May
Bob     5       10      30
Alice   20      5       20
Tony    10      10      15
Cathy   15      15      10

This file is identical to the combination of the previous two except that the file reference in the command, which was "example.dat" has been replaced with "-", which tells gnuplot to look for the data immediately following (which is where it is). I saved this file as eg2.gp and issue the command $: gnuplot -persist eg2.gp The result is a chart for the second column of the data file - that is, the March data only, plus 2 warning messages both of which say ""eg2.gp", line 16: warning: Skipping data file with no valid points". Here is the new image: stacked histogram eg2.png

The names on the x-axis have been lost. Line 16 in the eg2.gp file is the last line of data. I think that what is happening is that gnuplot is failing to loop back up to the beginning of the data part of the file when it starts to build the second row of boxes in the histogram. The question is, how can I instruct gnuplot to do that?

1

1 Answers

0
votes

You cannot read the same data from stdin ("-") multiple times. You would need to repeat the data as many times as you need it, separated by a line with e.

Since version 5.0 gnuplot has data blocks which allow you to use inline data multiple times:

$data <<EOD
Name    March   April   May
Bob     5       10      30
Alice   20      5       20
Tony    10      10      15
Cathy   15      15      10
EOD

set title "Test chart for row stacked histogram" 
set style data histograms
set style histogram rowstacked
set boxwidth 0.6
set style fill solid 1.0 border -1
set key invert autotitle columnhead
set yrange [:50]
set ylabel "Number of Referrals"
set ytics 10

plot $data using 2, '' using 3, '' using 4:xtic(1)

And you don't need to specify an explicit title. With set key autotitle columnhead the titles are read from the data.