gnuplot - draw stacked line chart - charts

I work with in-house benchmark tool. I use gnuplot (gnuplot 4.6 patchlevel 4) for visualization.
I need to represent results (method execution times for several runs) as stacked line chart, something like this:
Here is an excerpt from my .tsv data file:
Run MethodA MethodB MethodC
1 192 171 152
2 227 178 161
...
10 229 161 149
And the script I'm using:
#!/usr/bin/gnuplot -p
reset
clear
set terminal png size 640,480
set output "timings.png"
set key top left outside horizontal autotitle columnhead
set title "Third-party REST calls"
set xlabel "Run (ordinal)"
set xtics nomirror scale 0
set ylabel "Time (milliseconds)"
set ytics out nomirror
set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set boxwidth 0.75
plot "timings.tsv" using 2:xticlabels(1) , "" using 3, "" using 4
I get the following result:
Yes, it's not a line chart but histogram (I need to represent percentage of execution time of each method). I need slightly different result (the same histogram, not with boxes but with lines which connect boxes tops and with filling below lines), like this:
I'm aware of approach with filledcurve's (for instance, described there Creating a Filled Stack Graph in GNUPlot), but in that approach you need to sum values explicitly.
Is it possible to draw filled areas instead of boxes via gnuplot, e.g. convert histogram into stacked line chart?

You do need to sum values explicitly, but this is not a big issue. You can script it easily:
firstcol=2
cumulated(i)=((i>firstcol)?column(i)+cumulated(i-1):(i==firstcol)?column(i):1/0)
plot "file.dat" using 1:(cumulated(4)), "" using 1:(cumulated(3)), "" using 1:(cumulated(2))

Related

gnuplot reading from data to plot bar chars

I have a file with two columns
9 5
10 3
11 0
12 25
13 50
14 80
etc
What is the best way to plot bar char using gnuplot? Is using subprocess with gnuplot the best way? Ideally the graph should be in .pdf and in png as I want to put these on a website later on.
I would really appreciate your input/advise.
Here is an example, how to plot a bar chart with gnuplot using the plotting style with boxes
set terminal pngcairo size 1000,800 font ',12'
set output 'output.png'
set xlabel 'xlabel' font ',18'
set ylabel 'ylabel' font ',18'
set boxwidth 0.9
set style fill solid 0.3
set offset 0.5, 0.5, 10, 0
plot 'data.txt' with boxes linewidth 3 title ''
With the data you showed, this gives the following output image:

Columnstacked histograms in gnuplot from multiple files

I'm trying to use gnuplot to view some profiling data; I have several files, each of the following format:
file_runXX.dat:
elapsed time, stage
elapsed time, stage
For example:
0 foo
1 step_1
1.5 step_2
2.3 step_3
and
0 bar
0.75 step_1
1.3 step_2
2.1 step_3
To plot them, I use:
set style data histogram
set style histogram columnstack
plot for [i=1:2] sprintf("%02d.log", i) using 1
And I get a graph with two vertical bars: at x=0 I have a bar going from y=0 to y=1, then y=1 to y=1.5 and y=1.5 to y=2.3. At x=1, I have the same data from the second file.
Two questions:
(a) Is this the proper way to do this (i.e., it works, but is there something better?), and
(b) How can I set the xlabels to read "foo" and "bar" (see column 2, row 1, of each file)? I've tried messing around with using 1:xtic(2) or title columnheader and a few other options, but it seems that's only usable if I have one file containing both timestamps (I'm not sure I can do this, since I sometimes have step 2a in one file but not in the other; yes, I'm aware that this can mean the colors are not going to be uniform between bars).
Thanks
or you could transpose the data:
#label step_1 step_2 step_3
foo 1 1.5 2.3
bar .75 1.3 2.1
... and then use following commands:
set style data histograms
set boxwidth .7
set style histogram rowstacked
plot for [COL=2:4] "all.dat" using COL:xticlabels(1)
this adds a legend which you can suppress or customize.
you could combine all data in one tab-separated file all.dat:
foo bar
1 .75
1.5 1.3
2.3 2.1
and then use following commands:
set style data histograms
set style histogram columnstacked
set boxwidth .7
plot for [COL=1:2] "all.dat" using COL title columnhead

translate matlab plot to gnuplot 3d

I have a matrix of fft data over time, 8192 rows of data x 600 columns of time. The first column is a frequency label, the first row is shown below but doesn't actually exist in the data file, neither do the spaces, they are shown just for ease of reading.
Frequency, Sec1, Sec2, Sec3...Sec600
1e8, -95, -90, -92
1.1e8, -100, -101, -103
...
It is plotted in matlab with the following code (Apologies to other posters, I grabbed the wrong matlab code)
x is a matrix of 8192 rows by 600 columns, f is an array of frequency labels, FrameLength = 1, figN = 3
function [] = TimeFreq(x,f,FrameLength,figN)
[t,fftSize] = size(x);
t = (1:1:t) * FrameLength;
figure(figN);
mesh(f,t,x)
xlabel('Frequency, Hz')
ylabel('time, sec')
zlabel('Power, dBm')
title('Time-Freq Representation')
I cant quite figure out how to make it work in gnuplot. Here is a sample image of what it looks like in Matlab: http://imagebin.org/253633
To make this work in gnuplot, you'll want to take a look at the splot (for "surface plot") command. You can probably figure out quite a lot about it just by running the following commands in your terminal:
$ gnuplot
gnuplot> help splot
Specifically, you want to read the help page shown by running (after the above, when the prompt asks for a subtopic): datafile. That should tell you enough to get you started.
Also, the answers to this question might be helpful.
so here is the gnuplot command script that I ended up using. It has some additional elements in it that weren't in the original matlab plot but all the essentials are there.
set term png size 1900,1080
set datafile separator ","
set pm3d
# reverse our records so that time moves away from our perspective of the chart
set xrange[*:*] reverse
# hide parts of the chart that would make the 3d view look funny
set hidden3d
# slightly roate our perspective and compress the z axis
set view 45,75,,0.85
set palette defined (-120 "yellow", -70 "red", -30 "blue")
set grid x y z
set xlabel "time (secs)"
set ylabel "frequency"
set zlabel "dBm"
# plot all the data
set output waterfall.png
splot 'waterfall.csv' nonuniform matrix using 1:2:3 with pm3d lc palette

Gnuplot Stacked Histogram in Black and White

I'm trying to print stacked histogram from gnuplot into black and white png. By this I mean I would like each region in the bar to be hatched or checked alongside the legend on the side; that way when it's printed people can still identify the regions.
I've tried the following but it just leaves me with big black blobs!
set term pngcairo mono size 750, 300
set output \"pies/interesting.png\"
set style data histograms
set style histogram rowstacked
set boxwidth 1 relative
set style fill solid 1 border -1
set yrange [0:100]
set ytics 10
set datafile separator \",\"
plot './functions-$MODE.csv.t' using 1 t \"\", for [i=9:13] '' using (100.*column(i)/column(15)) ti column(i)
How do I make my regions hatched?
Apologies for answering my own question....I've realised that a stacked histogram behaves like a normal histogram: thus if I use the styles found on this page: http://gnuplot.sourceforge.net/demo/fillstyle.html I can customise to how I would like: setting "set style fill pattern border" :
set term pngcairo mono size 750, 300
set output \"pies/interesting.png\"
set style data histograms
set style histogram rowstacked
set boxwidth 1 relative
set style fill pattern border
set datafile separator \",\"
plot './functions-$MODE.csv.t' using 1 t \"\", for [i=9:13] '' using (100.*column(i)/column(15)) ti column(i)
Results in the desired effect.

Plotting horizontal lines across histogram bars

I have an experiment where I measured the performance of some algorithms relative to three baselines. I'd therefore like to plot histograms for the algorithms, with horizontal lines of various styles drawn through the histogram bars to show the baselines.
Below is an example which I produced by manually drawing horizontal lines on a graph produced by Gnuplot. The histograms "sentence" and "document" represent the algorithms I tested, and "mono", "random", and "MFS" are the baselines.
Is there some way I can do this within Gnuplot itself? If not, can anyone recommend another tool which can do this? Or perhaps there's a better visualization technique I should be using instead?
This is definitely possible. Here's a little example that I cooked up:
First, the datafile "data.dat":
#histograms
1 3 stack1
2 2 stack2
3 1 stack3
#mono
.6
.6
1.5
1.5
3.1
3.1
Now the gnuplot script to plot it:
set yrange [0:*]
set style data histograms
set style histogram cluster gap 1
IDX=-1
xpos(x)=(IDX=IDX+1, IDX%2==0)?(IDX/2-.5):(IDX/2+.5)
set style fill solid
plot 'data.dat' index "histograms" u 1:xtic(3) title "column1", \
'' index "histograms" u 2 title "column2", \
'' index "mono" u (xpos($1)):1 w lines ls -1 title "mono"
This is a little more tricky than my last version. When plotting a cluster of histograms, each cluster is centered on an integer starting at 0 and incrementing by 1 for each cluster (regardless of your setting for xtics and labels). What I've done is used that information to simplify the datafile. Now this plot command plots 2 different data sets as histograms (taken from each column in the "histogram" portion of the datafile), the first one adds the xtic labels. Then the tricky part: I write a function which has side-effects (gnuplot inline-functions are new in gnuplot 4.4 I think). Each time it is called, the value for the variable IDX is incremented -- So, the current position on the xrange is always IDX/2. This function alternates between returning IDX/2-.5 and IDX/2+.5. Note that to create another dataset random, you'll need another function xpos2 which is the same as xpos1 except it uses a separate iterator.