stacked graph with gnuplot - gnuplot

My data file looks like this
A 20120301 4
A 20120302 3
B 20120301 5
B 20120302 6
C 20120303 5
except there are many more than just A,B,C and I want to create a stacked graph with gnuplot (similar to the "Stacked histograms" from the gnuplot demos)
20120301 = (A:4 + B:5)
20120302 = (A:3 + B:6)
20120303 = (C:5)
So far I could not convince plot to read the data in that format. Do I have re-arrange the data file for this? Or is there a way for gnuplot to read the data in that format?

I think I've managed to beat it into a form that will work (you'll need at least gnuplot 4.3):
set boxwidth 0.75 absolute
set style fill solid 1.00 border lt -1
set datafile missing '-'
set style histogram rowstacked
set style data histograms
set yrange [0:]
plot for [i=2:4] 'test.dat' u i,'' u (0.0):xtic(1) notitle
and here's the datafile test.dat
#date A B C
#missing data is marked by a minus sign
20120301 4 5 -
20120302 3 6 -
20120303 - - 5
Phew! I've never been much good with gnuplot when it comes to histograms. Hopefully this will work for you (Sorry about the change to your datafile).

Related

Is there any way to visualize the field on adaptive mesh with gnuplot?

I am a beginner in gnuplot. Recently I tried to visualize a pressure field on adaptive mesh.
Firstly I got the coordinates of nodes and center of the cell and the pressure value at the center of the cell.
And, I found something difficult to deal with. That is the coordinates in x and y directions are not regular, which made me feel hard in preparing the format of source data. For regular and equal rectangular case, I can do something just like x-y-z format. But is there any successful case in adaptive mesh?
I understand that you have some x,y,z data which is in no regular grid (well, your adaptive mesh).
I'm not fully sure whether this is what you are looking for, but
gnuplot can grid the data for you, i.e. inter-/extrapolating your data within a regular grid and then plot it.
Check help dgrid3d.
Code:
### grid data
reset session
# create some test data
set print $Data
do for [i=1:200] {
x = rand(0)*100-50
y = rand(0)*100-50
z = sin(x/15)*sin(y/15)
print sprintf("%g %g %g",x,y,z)
}
set print
set view equal xyz
set view map
set multiplot layout 1,2
set title "Original data with no regular grid"
unset dgrid3d
splot $Data u 1:2:3 w p pt 7 lc palette notitle
set title "Gridded data"
set dgrid3d 100,100 qnorm 2
splot $Data u 1:2:3 w pm3d
unset multiplot
### end of code
Result:
If you have the size of each cell, you can use the "boxxyerror" plotting style. Let xdelta and ydelta be half the size of a cell along the x-axis and y-axis.
Script:
$datablock <<EOD
# x y xdelta ydelta pressure
1 1 1 1 0
3 1 1 1 1
1 3 1 1 1
3 3 1 1 3
2 6 2 2 4
6 2 2 2 4
6 6 2 2 5
4 12 4 4 6
12 4 4 4 6
12 12 4 4 7
EOD
set xrange [-2:18]
set yrange [-2:18]
set palette maxcolors 14
set style fill solid 1 border lc black
plot $datablock using 1:2:3:4:5 with boxxyerror fc palette title "mesh", \
$datablock using 1:2 with points pt 7 lc rgb "gray30" title "point"
pause -1
In this script, 5-column data (x, y, xdelta, ydelta, pressure) is given for "boxxyerror" plot. To colorize the cells, the option "fc palette" is required.
Result:
I hope this figure is what you are looking for.
Thanks.

gnuplot histogram chart with overlap

I would like to plot a bar chart or histogram like this in gnuplot.
I tried set style histogram rowstacked which is a start but it adds the columns on top of each other while I need them overlapped. Next is the issue of transparent color shading.
Thanks for your feedback.
UPDATE: user8153 asked for additional data.
The set style histogram clustered gap 0.0 is doing the cluster mode of the histogram bars. If you blur the eye it sort-of shows what I want but with overlap and transparent shading.
The only other histogram modes given in the docs are rowstacked and columnstacked. I never got a plot out of columnstacked so I discarded it. Now rowstacked stacks the histogram bars.
The overlay appearance is there but it is wrong. I don't want the stacked appearance. The histograms have to overlay.
Code :
set boxwidth 1.0 absolute
set style fill solid 0.5 noborder
set style data histogram
set style histogram clustered gap 0.0
#set style histogram rowstacked gap 0.0
set xtics in rotate by 90 offset first +0.5,0 right
set yrange [0:8000]
set xrange [90:180]
plot 'dat1.raw' using 3 lc rgb 'orange', \
'dat2.raw' using 3 lc rgb 'blue', \
'dat3.raw' using 3 lc rgb 'magenta'
Thanks for your feedback.
Given a sample datafile test.dat
-10 4.5399929762484854e-05
-9 0.0003035391380788668
-8 0.001661557273173934
-7 0.007446583070924338
-6 0.02732372244729256
-5 0.0820849986238988
-4 0.20189651799465538
-3 0.4065696597405991
-2 0.6703200460356393
-1 0.9048374180359595
0 1.0
1 0.9048374180359595
2 0.6703200460356393
3 0.4065696597405991
4 0.20189651799465538
5 0.0820849986238988
6 0.02732372244729256
7 0.007446583070924338
8 0.001661557273173934
9 0.0003035391380788668
10 4.5399929762484854e-05
you can use the following commands
set style fill transparent solid 0.7
plot "test.dat" with boxes, \
"test.dat" u ($1+4):2 with boxes
to get the following result (using the pngcairo terminal):
Using transparency as in user8153's solution is certainly the easiest way to visualize an overlap of two histograms.
This works even if the two histogram do not have identical bins or x-data-ranges.
However, the color of the overlap is pretty much bound to the colors of the two histogram and the level of transparency. Furthermore, if you want to show the overlap in the key you have to do it "manually".
Here is a solution where you can choose an independent color for the overlap area.
The overlap is basically the minimum y-value from both histograms for each x-value.
For this you need to compare the y-values for each x-value. This can be done in gnuplot with some "trick" by merging the two files line by line. This requires the data in a datablock (how to get it there from a file). Since this merging procedure is using indexing of datablock lines, it requires gnuplot>=5.2.0.
This assumes that you have the same x-range and bins for each histogram. If this is not the case, you have to implement some further steps.
Script: (works with gnuplot>=5.2.0, Sept. 2017)
### plot overlap of two histograms
reset session
# create some random test data
set samples 21
f(x,a,b) = 1./(a*(x-b)**4+1)
set table $Data1
plot '+' u 1:(f(x,0.01,-2)) w table
set table $Data2
plot '+' u 1:(f(x,0.02,4)) w table
unset table
set boxwidth 1.0
set grid y
set ytics 0.2
set multiplot layout 2,1
set style fill transparent solid 0.3
plot $Data1 u 1:2 w boxes lc 1 ti "Data1", \
$Data2 u 1:2 w boxes lc 2 ti "Data2"
set print $Overlap
do for [i=1:|$Data1|] { print $Data1[i].$Data2[i] }
set print
set style fill solid 0.3
plot $Data1 u 1:2 w boxes lc 1 ti "Data1", \
$Data2 u 1:2 w boxes lc 2 ti "Data2", \
$Overlap u 1:($2>$4?$4:$2) w boxes lc "red" ti "Overlap"
unset multiplot
### end of script
Result:

Gnuplot stacked histogram skipping the first bin

I'm trying to use gnuplot to plot a stacked histogram of some data but it skips the first bin (the first row of the data file).
The data is:
1 0.2512 0.0103 0.9679
2 0.4730 0.2432 0.8468
3 0.6669 0.2826 0.6895
4 0.6304 0.2268 0.7424
And the plot code is
set title "Data"
set key invert reverse Left outside
set key autotitle columnheader
set style data histogram
set style histogram rowstacked
set style fill solid border -1
#set boxwidth 0.75
plot 'data.dat' using 2:xtic(1) title 'X', '' using 3 title 'Y', '' using 4 title 'Z'
The output is. I checked it and it correctly displays the data of the 2nd, 3rd and 4th rows of the data file. Why am I missing the first bin..?
Thanks a lot!
I already checked this with no help: Using gnuplot for stacked histograms
As it turns out, it was a very simple mistake, that I've fixed mostly thanks to Azad comment about the titles.
The new code is:
set title "Position error along the three axis"
set key invert reverse Left outside
#set key autotitle columnheader
set style data histogram
set style histogram rowstacked
set style fill solid border -1
#set boxwidth 0.75
plot 'data.dat' using 2:xtic(1), '' using 3, '' using 4
Titles have been removed from the code. Gnuplot was taking the first row (which should have been the first bin) as the titles and then it was overwritten by the title 'X' etc.
The new data looks like this:
0 X Y Z
1 0.2512 0.0103 0.9679
2 0.4730 0.2432 0.8468
3 0.6669 0.2826 0.6895
4 0.6304 0.2268 0.7424
This fixed the problem, now all the bins are correctly displayed!

Gnuplot: draw error bars of data points outside plotting range

If I set a specific yrange and plot in a pdf terminal with this plot command:
plot "data.dat" u 1:4:5:6 w yerrorbars pt 6 ps 0.5 t "R_t"
errorbars that belong to data points outside the yrange, but end inside the yrange are not shown.
How do I force gnuplot to draw those. I already tried "set clip one/two"
The only workaround I found is to plot the data 3 times, once for the central point and once for each side of the error bar.
Use "-" as symbol for the errorbars and use their own "errorbars" to draw a line to the central point.
You could use multiplot to achieve this.
Set your plot to have zero margins, so the axes are on the border of the canvas, and switch of all tics and borders for the first plot.
Switch on the axes, tics etc. again, and do an empty plot that you set at the correct position using set size and set origin. You'll have to do some math to calculate the exact position.
#MaVo159, you can reduce it to plotting only twice by using with yerrorbars and with vectors (check help vectors). You need to set the proper arrow style, check help arrowstyle.
However, this works only for gnuplot>=5.2.3, for earlier versions there seems to be a bug which plots the arrowhead at the wrong side for some of the vectors extending the graph.
You nevertheless have to plot once with yerrorbars in order to get the proper legend.
Script: (works for gnuplot>=5.2.3, May 2018)
### plot errorbars from points outside the range
reset
$Data <<EOD
1 9 5.11 8.32
2 8 6.20 9.22
3 6 5.31 6.31
4 5 4.41 5.51
5 4 3.31 4.71
6 2.9 2.81 3.71
7 2 1.11 3.41
EOD
set yrange[3:7]
set offsets 1,1,0,0
set style arrow 1 heads size 0.05,90 lw 2 lc 1
set multiplot layout 2,1
plot $Data u 1:2:3:4 w yerrorbars pt 6 ps 2 lw 2
plot $Data u 1:2:3:4 w yerrorbars pt 6 ps 2 lw 2, \
'' u 1:3:(0):($4-$3) w vec as 1 notitle
unset multiplot
### end of script
Result:
You could modify your data file: Because the central value of the data point is outside the plot range you could set it equal to the errorbar's end point that would be still visible in your plot.
Example:
plot range: set yrange[-2:2]
data point: 1, -3, -1, -4 (x, y, ylow, yhigh)
set data point to: 1, -1, -1, -4
Attention: Since you have to edit your data file you should
Make a copy of the original data file
Be very careful when editing the file
Keep in mind, that when changing the plot range such that the central
value of the data point becomes visible you have to use the original data point. Otherwise you will see the correct error bar but there will be no central value plotted. (this is equivalent to setting 'point type' to 0)

x range for non-numerical data in Gnuplot

When running the following script, I get an error message:
set terminal postscript enhanced color
set output '| ps2pdf - histogram_categorie.pdf'
set auto x
set key off
set yrange [0:20]
set style fill solid border -1
set boxwidth 5
unset border
unset ytic
set xtics nomirror
plot "categorie.dat" using 1:2 ti col with boxes
The error message that I get is
smeik:plots nvcleemp$ gnuplot categorie.gnuplot
plot "categorie.dat" using 1:2 ti col with boxes
^
"categorie.gnuplot", line 13: x range is invalid
The content of the file categorie.dat is
categorie aantal
poussin 13
pupil 9
miniem 15
cadet 15
junior 6
senior 5
veteraan 8
I understand that the problem is that I haven't defined an x range. How can I make him use the first column as values for the x range? Or do I need to take the row numbers as x range and let him use the first column as labels? I'm using Gnuplot 4.4.
I'm ultimately trying to get a plot that looks the same as the plot I made before this one. That one worked fine, but had numerical data on the x axis.
set terminal postscript enhanced color
set output '| ps2pdf - histogram_geboorte.pdf'
set auto x
set key off
set yrange [0:40]
set xrange [1935:2005]
set style fill solid border -1
set boxwidth 5
unset border
unset ytic
set xtics nomirror
plot "geboorte.dat" using 1:2 ti col with boxes,\
"geboorte.dat" using 1:($2+2):2 with labels
and the content of the file geboorte.dat is
decennium aantal
1940 2
1950 1
1960 3
1970 2
1980 3
1990 29
2000 30
the boxes style expects that the x-values are numeric. That's an easy one, we can give it the pseudo-column 0 which is essentially the script's line number:
plot "categorie.dat" using (column(0)):2 ti col with boxes
Now you probably want the information in the first column on the plot somehow. I'll assume you want those strings to become the x-tics:
plot "categorie.dat" using (column(0)):2:xtic(1) ti col with boxes
*careful here, this might not work with your current boxwidth settings. You might want to consider set boxwidth 1 or plot ... with (5*column(0)):2:xtic(1) ....
EDIT -- Taking your datafiles posted above, I've tested both of the above changes to get the boxwidth correct, and both seemed to work.

Resources