Gnuplot: fill area bounded by curves left/right? - gnuplot

I have a dataset that defines two curves, and I want to fill the area between them. However, contrary to the standard situation, the abscissa is to be plotted on the vertical axis and the ordinates on the horizontal one; the abscissa indicates depth, this is a common plotting format in geophysics. In other words, I want something like
plot 's.dat' u 1:2:3 w filledcurves
but with swapped axes so that the filled area is bounded not at the top and bottom but to the left and right by the curves as seen in
plot 's.dat' u 2:1,'s.dat' u 3:1
My dataset is like this:
0. -1.776 -0.880
160. -1.775 -0.882
160. -1.692 -0.799
320. -1.692 -0.800
320. -1.531 -0.634
480. -1.534 -0.637
480. -1.286 -0.394
Is this possible in Gnuplot?
Thomas

This is a totally different solution using 3D plot style "with zerror".
You will need current gnuplot (version 5.2) for this. The plot style was really not designed for this so there are some difficulties (e.g. x tic marks invisible because drawn perpendicular to the plane of the plot, all tic labels requiring an offset for readability).
#
# [mis]use 3D plot style "with zerror" to create a plot of the xz
# plane with area fill between two sets of data points with
# equal coordinates on the vertical axis (x) but contrasting
# values on the horizontal axis (z).
#
set view 270, 0
set view azimuth -90
set xyplane at 0
unset ytics
set ztics offset 4, -2 out
set xtics offset 4
splot 's.dat' using 1:(0):(0.5*($2+$3)):2:3 with zerror notitle

If there is some value of x which is guaranteed to lie between the two curves then you can plot in two halves. For the data you show, x=-1 would be a suitable value and the plot command would be:
plot 's.dat' u 2:1 with filledcurve x=-1 lt 3, \
's.dat' u 3:1 with filledcurve x=-1 lt 3
If the requirement for a constant intermediate x value can only be
satisfied piece-wise, e.g.
x=-1 for (0<y<500), x=0 for (500<y<1000)
then it may nevertheless be possible to construct a graph by stacking
the piecewise sections.

A simple way would be to define a closed line and fill it. For this, you take column 2 and add the reversed column 3. You probably need gnuplot >=5.2 for this.
Code:
### fill between vertical curves
reset session
$Data <<EOD
0. -1.776 -0.880
160. -1.775 -0.882
160. -1.692 -0.799
320. -1.692 -0.800
320. -1.531 -0.634
480. -1.534 -0.637
480. -1.286 -0.394
EOD
set print $Outline
do for [i=1:|$Data|] {
print sprintf("%s %s", word($Data[i],2), word($Data[i],1))
}
do for [i=|$Data|:1:-1] {
print sprintf("%s %s", word($Data[i],3), word($Data[i],1))
}
set print
plot $Outline w filledcurve lc rgb "green"
### end of code
Result:

Related

Plotting intersecting lines in GNUplot

I haven't been able to find any example of what I'm trying to do in GNUplot from raking docs and demos.
Essentially I want to plot the Blue, Green, and Red lines I manually drew on this output (for demonstration) at the 10/50/90% marks.
EDIT: For clarity, I'm looking to determine where the distribution lines hit the cumulative distribution at 0.1/0.5/0.9 to know which co-ordinates to draw the lines at. Thanks!
set terminal png size 1600,800 font "Consolas" 16
set output "test.png"
set title "PDF and CDF - 1000 Simulations"
set grid y2
set ylabel "Date Probability"
set y2range [0:1.00]
set y2tics 0.1
set y2label "Cumulative Distribution"
set xtics rotate by 90 offset 0,-5
set bmargin 6
plot "data.txt" using 1:3:xtic(2) notitle with boxes axes x1y1,'' using 1:4 notitle with linespoints axes x1y2
Depending on the number of points in your cumulative data curve you might need interpolation. The following example is chosen such that no original data point will be at your levels 10%, 50%, 90%. If your data is not steadily increasing, it will take the last value which matches your level(s).
The procedure is as follows:
plot your data to a dummy table.
check when Level is between to successive y-values (y0,y1).
remember the interpolated x-value in xp.
draw arrows from the borders of the graph to the point (xp,Level) (or instead use the partly outside rectangle "trick" from #Ethan).
Code:
### linear interpolation of data
reset session
set colorsequence classic
set key left
# create some dummy data
set sample 10
set table $Data
plot [-2:2] '+' u 1:(norm(x)) with table
unset table
Interpolate(yi) = x0 + (x1-x0)*(yi-y0)/(y1-y0)
Levels = "0.1 0.5 0.9"
do for [i=1:words(Levels)] {
Level = word(Levels,i)
x0 = x1 = y0 = y1 = NaN
set table $Dummy
plot $Data u (x0=x1,x1=$1,y0=y1,y1=$2, (y0<=Level && Level<=y1)? (xp=Interpolate(Level)):NaN ): (Level) w table
unset table
set arrow i*2 from xp, graph 0 to xp,Level nohead lc i
set arrow i*2+1 from xp,Level to graph 1,Level nohead lc i
}
plot $Data u 1:2 w lp pt 7 lc 0 t "Original data"
### end code
Result:
It is not clear if you are asking how to find the x-coordinates at which your cumulative distribution line hits 0.1, 0.5, 0.9 (hard to do so I will leave that for now) or asking how to draw the lines once you know those x values. The latter part is easy. Think of the lines you want to draw as the unclipped portion of a rectangle that extends off the plot to the lower right:
set object 1 rectangle from x1, 0.1 to graph 2, -2 fillstyle empty border lc "blue"
set object 2 rectangle from x2, 0.1 to graph 2, -2 fillstyle empty border lc "green"
set object 3 rectangle from x3, 0.1 to graph 2, -2 fillstyle empty border lc "red"
plot ...

Gnuplot histogram gap does nothing

I have a gnuplot script which plots a histogram. I used the following syntax:
set style data histogram
set style histogram cluster gap 2
set style fill solid
set logscale y
rgb(r,g,b) = int(r)*65536 + int(g)*256 + int(b)
plot 'histogram_data' using (column(0)):2:(0.5):(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable
What the last line does is: using column 1 as x labels, column 2 as the height of the histogram bars, 0.5 as box width, and columns 3, 4 and 5 as the rgb values to colour the bars.
Now, the problem is that modifying the gap parameter in line 2 does not change in any way the spacing between bars, even though as far as I understand that is the correct way to adjust such spacing. I am using gnuplot 4.6 patchlevel 4.
I found a way to do this with boxes, though I do not consider it very clean:
plot 'histogram_data' u (column(0)*2+1):2 w boxes notitle lc rgb 'white',\
'histogram_data' u (column(0)*2):2:(rgb($3,$4,$5)):xticlabels(1) w boxes notitle lc rgb variable;
This command is plotting all the data of the main plot on even slots and a white box on odd slots. So the first line in the plot command is plotting the gaps between every box of the plot (the width of these gaps can be specified using the boxwidth property I think but I haven't tested this), while the second line is drawing the actual plot.
I could not find a way to do this with the histogram plotting style, keeping the variable colours specified in the data file.

gnuplot - filled curve with palette color

I'm trying to plot a histogram in gnuplot, where the bars are characterized by their color (intensity) instead of their height (frequency). To do so, I want to fill each bar with a color corresponding to the third column (which stands for this intensity) of a data file, defined in a palette. All the bars have the same height y=1 and the same width dx=1. The important part of the script looks like
plot for [ii=0:N] 'data.dat' index ii u 1:2:3 w filledcu y1=0 lc palette
My problem is that the ii=0 takes the right color of the palette and fills the first bar, but from there on all the others are the same color ( same intensity ) than ii=0.
My data file looks like:
X Y Intensity
1 1 0.6
2 1 0.6
...
Any idea to fill with the right color ?
The filledcurves plotting style doesn't support color gradients, see Gnuplot filledcurves with palette.
Since you have the same height for every bar, you can use the boxes plotting style:
set style fill solid noborder
plot 'data.dat' using 1:2:3 with boxes lc palette

Reduce distance between points in splot

I have this gnuplot script
reset
set palette model RGB defined (0 "gray", 0.1 "white", 0.33 "yellow", 0.66 "orange", 1 "red")
set xlabel "x"
set ylabel "y"
set view map
set border 0
unset xtics
unset ytics
splot file_name u 1:2:5:xtic(3):ytic(4) w points ps 5 pt 5 palette
And this is the result:
How can I remove the distance between the points so that I end up with a set of adjacent squares? I want to plot a heatmap with a square for each point in my grid file.
EDIT
The correct way to plot a "grid" heatmap as per #andyras answer is:
set pm3d map
plot file_name u 1:2:5:xtic(3):ytic(4) with image
Which gives this image:
gnuplot offers a third way to plot a heatmap based on connecting points of a 3d surface, rather than grid cells. That is, the x,y coordinates at columns 1,2 are used as corners, or connecting points, of a surface mesh and the colors used in each region are the average of the RGB/HSV values for the 4 defining corners:
set pm3d map
splot file_name u 1:2:5:xtic(3):ytic(4)
I usually go with the options
set pm3d map
plot file_name u 1:2:5:xtic(3):ytic(4) with image
for gridded data (it makes a smaller file if you use vector formats). I suspect your problem may be to do with the fact that you specify a point style and size for your splot. So, you could try setting the pm3d map option and using splot without the point specification, or plot ... with image.

How to plot horizontal lines from a single point

I have a dataset(a file) with a single column and some entries. I wish to plot a horizontal line for each entry, where each entry is the y-axis value of the line. How can I do this?
What a strange plot you're trying to make. You can do it using vectors without heads:
set style arrow 1 nohead
plot 'datafile' using (0.0):1:(0.0):(2.0) with vectors arrowstyle 1
Or, in gnuplot shorthand (just for fun):
p 'datafile' u (0.0):1:(0.0):(2.0) w vec as 1
adjust yrange and xrange accordingly

Resources