Heatmap with Gnuplot on a non-uniform grid - gnuplot

I would like to create a heatmap with gnuplot based on a non-uniform grid, meaning that my x axis bins do not have all the same width, and I can't figure out how to do that because when I plot my data with for example "with image" I get uniformly sized boxes which do no correspond to my coordinates at all (because "image" treats the data just as matrix I guess). So I would like to find a method to get non-uniform boxes which are also positioned in the right place on the Cartesian plane.
My data look something like this:
1 1 0.2
1 2 0.8
1 3 0.1
1 4 0.2
2 1 0.7
2 2 0.2
2 3 0.3
2 4 0.1
5 1 0.2
5 2 0.4
5 3 0.1
5 4 0.9
7 1 0.3
7 2 0.2
7 3 0.9
7 4 0.6
If I run this command on Gnuplot
set xrange [1:10]
p 'mydata.dat' with image
I get an image with 16 boxes that have the same width and height (apparently I don't have enough "reputation" on Stackoverflow to post an image, otherwise I would), but ideally I would like the boxes to have different widths and be in the right place on the plane. For example the first box should range from 1 to 2, the second one from 2 to 5, the third one from 5 to 7, and the last one from 7 to 10 (which is why I wrote set xrange [1:10]).
Could anyone help me please? Thank you very much!

The easiest (maybe only viable) way is to add some dummy data points and use splot ... with pm3d. This plotting style handles heatmaps with general quadrangles.
The image plotting style plots one box (one big pixel) for each data point, while pm3d takes each data point as corner of one or more quadrangles. The color of each quadrangles is determined by the values of the corners and is adjustable with set pm3d corners2color.
So, in your case you need to expand the 4x4 matrix to a 5x5 matrix (expand to right and top), but select the lower left corner to determine the color set pm3d corners2color c1.
The changed data file is then:
1 1 0.2
1 2 0.8
1 3 0.1
1 4 0.2
1 5 0.5
2 1 0.7
2 2 0.2
2 3 0.3
2 4 0.1
2 5 0.5
5 1 0.2
5 2 0.4
5 3 0.1
5 4 0.9
5 5 0.5
7 1 0.3
7 2 0.2
7 3 0.9
7 4 0.6
7 5 0.5
10 1 0.5
10 2 0.5
10 3 0.5
10 4 0.5
10 5 0.5
To plot it use
set pm3d map corners2color c1
set autoscale fix
set ytics 1
splot 'mydata.dat' using 1:($2-0.5):3 notitle
The result with 4.6.3 is:
In general, the z-value of the dummy data points doesn't matter, but in the above script it should lay somewhere between minimum and maximum values to allow set autoscale fix to work properly on the color scale.
If you don't want to change the data file manually, you could do it with some script, but that's a different question.

Here is an alternative solution without splot ... pm3d, but with boxxyerror.
If you plot data it should go as automatic as possible and there should be no need to "invent" and manually add data.
The following solution (a little bit more complex) takes care about the widths (+/-dx) and heights (+/-dy) of the boxes according to the following principle:
if it is an "inner" box, take half the distance to the adjacent datapoint on that side
if it is an "outer" box, take half the distance to the adjacent "inner" datapoint
Here, x-distances are irregular and y-distances are regular, but y-distances could also be irregular.
Data: SO19294342.dat
1 1 0.2
1 2 0.8
1 3 0.1
1 4 0.2
2 1 0.7
2 2 0.2
2 3 0.3
2 4 0.1
5 1 0.2
5 2 0.4
5 3 0.1
5 4 0.9
7 1 0.3
7 2 0.2
7 3 0.9
7 4 0.6
Script: (works with gnuplot>=4.6.0, March 2012)
### heatmap with boxxyerror and variable box-sizes
reset
FILE = "SO/SO19294342.dat"
set style fill solid 1.0
set tics out
set size ratio -1
# extract x-positions
Xs = Ys = ''
Nx = Ny = 0
b = -1
stats FILE u (column(-1)!=b ? (Nx=Nx+1, Xs=Xs.sprintf(" %g",$1), b=column(-1)) : 0, \
column(-1)==0 ? (Ny=Ny+1, Ys=Ys.sprintf(" %g",$2)) : 0) nooutput
d(vs,n0,n1) = abs(real(word(vs,n0))-real(word(vs,n1)))/2
dn(vs,n) = (n==1 ? (n0=1,n1=2) : (n0=n,n1=n-1), -d(vs,n0,n1))
dp(vs,n) = (Ns=words(vs), n==Ns ? (n0=Ns-1,n1=Ns) : (n0=n,n1=n+1), d(vs,n0,n1))
plot FILE u 1:2:($1+dn(Xs,column(-1)+1)):($1+dp(Xs,column(-1)+1)):\
($2+dn(Ys,int(column(0))%Ny+1)):($2+dp(Ys,int(column(0))%Ny+1)):3 w boxxy palette notitle
### end of script
For gnuplot>=4.6.5 you could add :xtic(1):xtic(2) to the plot command to only show your x- and y-coordinates as x,y-ticlabels.
plot FILE u 1:2:($1+dn(Xs,column(-1)+1)):($1+dp(Xs,column(-1)+1)):\
($2+dn(Ys,int(column(0))%Ny+1)):($2+dp(Ys,int(column(0))%Ny+1)):3:\
xtic(1):ytic(2) w boxxy palette notitle
And for gnuplot>=5.0.0 you could add noextend to the ranges to avoid white areas on the sides:
set xrange[:] noextend
set yrange[:] noextend
Result: (created with gnuplot 4.6.0)

Related

gnuplot: transform axis of matrix plot with "every"

I have a problem with plotting matrices with gnuplot. I am plotting one row of matrix with every option like that
plot inputfile matrix every 1:1:(4+N*M+1):100:(4+N*(M+1)):100 with linespoint
where 100 is number of row. It gave me that result:
nearly good result
I would like to get xrange from 0 to 360, but when I use something like that
plot inputfile matrix using ($1*11.25):2 every 1:1:(4+N*M+1):100:(4+N*(M+1)):100 with linespoint
it doesen't work: wrong result
What can I do with it?
You don't provide data, so I create some for the following example.
As I understand you want to plot a certain row of a matrix and adjust the x-range.
Check help matrix every.
For example, in plot FILE u 1:2:3 matrix, 1 is the column, 2 the row and 3 is the (z)-value.
And in plot FILE u 1:3 matrix every ::c:r:c:r, c is the column and r is the row (counting starts from 0).
So the example below plots the 4th row and the x-range is adjusted from 0 to 360.
Code:
### plotting a certain row while adjusting the x-range
reset session
$Data <<EOD
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
EOD
set key top left
plot $Data u ($1*60):3 matrix every :::3::3 w lp pt 7
### end of code
Result:

How do you sum every nth column of data in gnuplot?

I would like to take an average over several columns of a data set in Gnuplot. The problem is that I want to average every other column (starting from the second column of my dataset). I was thinking of using every somehow but I still don't really understand when and where to use every. To help visualise my question: my data looks something like this:
x y1 z1 y2 z2
2 0.6 0 0.6 0
1 0.7 0 0.7 1
1 0.8 2 0.8 1
1 0.9 0 0.9 0
and I would like to average y1 and y2 and plot the result by doing something like:
stats filename nooutput
plot filename u 1:sum[col = every :2::2::STATS_columns] / ((STATS_columns-1)/2)
Not sure if this is anywhere close to doable though. Also, it would be nice to have a way of finding the number of columns used without any apriori knowledge of what the data looks like. In the example I have used my knowledge of the data to know that the average is over ((STATS_columns-1)/2) number of points.
Thank you for your response
From your code I assume you want to average y1 and y2 for each row and then plot it versus x (column 1). Since you have several identical x values, there would be another average, namely an average over the columns and over all identical x values.
I modified your data to better illustrate the difference.
I guess you were asking fot the red circles. The blue triangles are basically the average of the average, i.e. the average of the red points.
Check help summation and help smooth. sum has no step size with the index.
From gnuplot help:
sum [<var> = <start> : <end>] <expression>
Code:
### average over columns and smooth
reset session
$Data <<EOD
#x y1 z1 y2 z2
1 2.0 0 4.0 0
1 2.2 0 4.2 1
1 2.9 2 4.9 1
2 2.1 0 4.1 0
2 2.3 0 4.3 0
2 2.8 0 4.8 0
3 2.2 0 4.2 0
3 2.3 0 4.3 0
3 2.7 0 4.7 0
EOD
stats $Data nooutput
set offsets 0.5,0.5,0.5,0.5
Count = (STATS_columns-1)/2
plot $Data u 1:((sum[i=1:Count] column(i*2))/Count) w p pt 7 lc rgb "red" ti "average over y1,y2 columns for each row",\
$Data u 1:((sum[i=1:Count] column(i*2))/Count) smooth unique w p pt 9 lc rgb "blue" ti "average over y1,y2 for each x"
### end of code
Result:

Gnuplot: add infinity value to colorbox

I would like to plot a simple heatmap with Gnuplot which is very similar to this example:
unset key
set cbrange [0:6]
set xrange [0:10]
set yrange [0:5]
set size ratio -1
set cbtics out nomirror
set palette maxcolors 12 model RGB defined (0 '#1a9641', 1 '#a6d96a', 2 '#ffffbf', 3 '#fdae61', 4 '#d7191c')
$map1 << EOD
5.5 4.0 3.5 1.0 0.5 5.0 4.5 3.0 1.5 0.0
2.0 2.5 0.0 inf inf 4.5 3.0 0.5 0.0 1.5
0.5 0.0 0.5 inf inf 0.0 0.5 0.0 1.5 0.0
0.0 0.5 0.0 2.5 3.0 0.5 0.0 0.5 2.0 3.5
0.5 1.0 2.5 4.0 3.5 2.0 2.5 0.0 0.5 1.0
EOD
plot '$map1' using ($1+.5):($2+.5):($3) matrix with image
This is the corresponding plot:
As you can see the matrix contains infinity values. I would like to add an extra color (for example blue) to the colorbox especially for infinity values (the big red square in the middle should appear blue).
At first I thought that I just have to add one more color in the end of my defined color values for the colorbox. But this will result in an color transition between the last two colors (red and blue) because blue would be the associated color for the max value of cbrange. But the max value of non infinity values should stay 6 and red.
The result should look something like this:
Any ideas out there?
You'll need something like the following three lines, filling in colors 0-11 manually according to your desired gradient (I've used this site to generate gradients in the past.)
set cbrange [0:6.5]
set palette maxcolors 13 model RGB defined \
( 0 '#222222', 1 '#333333', 2 '#444444', \
3 '#555555', 4 '#666666', 5 '#777777', \
6 '#888888', 7 '#999999', 8 '#aaaaaa', \
9 '#bbbbbb', 10 '#cccccc', 11 '#dddddd', 12 '#dd0000')
set cbtics ("0" 0, "1" 1, "2" 2, "3" 3, "4" 4, "5" 5, "6" 6, "inf" 6.5)

Drawing polygons with holes using Gnuplot

Is there a way using Gnuplot 5.0 to plot a polygon with a hole with filledcurves?
Here are my test data:
# Outer ring
0 -2
-2 0
0 2
2 0
0 -2
# Inner ring
-0.5 0.5
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
And here is the result:
I know I could re-order the vertices in order to hide the connecting line (in fact the polygon frontier) between the outer and inner ring. But I will deal with machine-generated data, and I would prefer minimize the amount of data preprocessing.
In some other drawing programs, we can draw holes inside polygons by changing the winding-rule to even-odd. But I didn't find such option in gnuplot.
Finally, I cannot just draw the "hole" in white, since in my application I have several shapes to draw, And I want to see other shapes behind the "hole".
Why not just plot the hole in white on top of the shape?
Separate your date into
# shape.txt
0 -2
-2 0
0 2
2 0
0 -2
and
# hole.txt
-0.5 0.5
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
and then use
plot "shape.txt" u 1:2 w filledcurves, 'hole.txt' u 1:2 w filledcurves lc 'white'
The following solution does what you already mentioned in your question: It reorders the points of the inner polygon in such a way, that the endpoint of the outer polygon and the starting point of the inner polygon have minimal distance (hence no line across the inner polygon). It does is automatically with gnuplot only, so this still might be an acceptable solution for you.
Assumptions:
the outer curve is closed (start point = end point)
the outer curve does not have duplicated points
Procedure:
go through the data and when the first x and y values are found again, the outer structure is finished and the inner structure starts (row index stored in idx0)
when the inner structure starts, it looks for the minimum distance to the start/end point of the outer structure (x0,y0) (row index stored in idx1)
the data is plotted into a datablock $Hollow: first the outer as is and then the inner starting from idx1 to end, and then the inner starting from (idx0+2) to idx1.
In the example below red lines are drawn first to illustrate the empty polygon in the center. Tested with gnuplot 5.0.0.
For multiple holes in a filled curve you might want to check gnuplot: How to draw a filled area with hole? with a rather general but pretty lengthy solution.
Code:
### draw hollow polygon
reset session
$Data <<EOD
# Outer ring
0 -2
-2 0
0 2
2 0
0 -2
# Inner ring
-0.5 0.5
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
EOD
Distance(x0,y0,x1,y1) = sqrt((x1-x0)**2 + (y1-y0)**2)
GetIdxs(colX,colY) = LastOuter==1 ? ( d1=Distance(x0,y0,column(colX),column(colY)), \
d0==d0 ? (d1<dmin ? (idx1=column(0), dmin=d1) : 0) : \
(idx1=column(0),dmin=d1), d0=d1, LastOuter) : \
column(0)==0 ? (d0=d1=dmin=NaN,x0=column(colX),y0=column(colY)) : \
column(colX)==x0 && column(colY)==y0 && LastOuter==0 ? \
(idx0=column(0),x0=column(colX),y0=column(colY),LastOuter=1 ) \
: LastOuter
set table $Dummy
plot LastOuter=0 $Data u 1:2:(GetIdxs(1,2)):(d1) w table
set table $Hollow
plot $Data u 1:2 every ::::idx0 w table
plot $Data u 1:2 every ::idx1 w table
plot $Data u 1:2 every ::idx0+2::idx1 w table
unset table
plot for [i=-5:5] -x+i/5.lw 2 lc "red" notitle, \
$Hollow u 1:2 w filledcurves lc 1
### end of code
Result:
Just for better understanding the content of $Dummy and $Hollow.
$Dummy
0 -2 -2 nan
-2 0 0 nan
0 2 0 nan
2 0 0 nan
0 -2 1 nan
-0.5 0.5 1 2.54951
-0.5 -0.5 1 1.58114
0.5 -0.5 1 1.58114
0.5 0.5 1 2.54951
-0.5 0.5 1 2.54951
$Hollow
0 -2
-2 0
0 2
2 0
0 -2
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
-0.5 -0.5
My other solution is based on reordering the inner curve in order to avoid the connection line from outer to inner curve crossing the open area. However, this was rather cumbersome with dummy and extra tables and with a lengthy, confusing expression.
Here is a another much simpler solution:
It is based on variable linecolors, i.e. invisible (fully-transparent) lines, when you switch from the outer curve to the inner curve. This should be fine unless you want to create a SVG or DXF and use the graph as pattern for a cutter.
Prerequisites:
outer and inner curves must be closed (first point = last point)
you have to make sure that outer and inner curves have opposite directions (CW=clockwise and CCW=counterclockwise), this was the case in your example. Otherwise the area will not be hollow but filled.
there should be no empty line between inner and outer curves, but two empty lines between the shapes.
You can easily tell gnuplot when the outer curve ends: when the first x,y-coordinates appears again. It doesn't matter where the inner curve starts as long as the direction is reversed to the outer one.
Just for illustration, I plotted sin(x) and made the 1st structure opaque and 2nd and 3rd structure in semitransparent color.
Script: (works for gnuplot>=5.0.3)
### plot areas with holes
reset session
$Data <<EOD
0 -2 0 # outer CW
-2 0 0
0 2 0
2 0 0
0 -2 0
-0.5 0.5 1 # inner CCW
-0.5 -0.5 1
0.5 -0.5 1
0.5 0.5 1
-0.5 0.5 1
-0.5 -1 0 # outer CCW
-0.1 -0.1 0
-1 0.5 0
-1.5 -1 0
-0.5 -1 0
-0.8 0.2 1 # inner CW
-0.2 -0.1 1
-0.6 -0.8 1
-1.2 -0.8 1
-0.8 0.2 1
0 -1 0 # outer CCW
1 -1 0
1.3 0 0
0.3 0 0
0 -1 0
1.1 -0.2 1 # inner CW
0.9 -0.8 1
0.2 -0.8 1
0.4 -0.2 1
1.1 -0.2 1
EOD
myColors = "0x00ff00 0x770000ff 0x77ff0000"
myColor(i) = i+1>words(myColors) ? 0x000000 : int(word(myColors,i+1))
myColorX(i) = (x0=x1, x1=$1, y0=y1, y1=$2, \
$0==0 ? (fx=x1,fy=y1) : x1==fx && y1==fy ? last=1 : 0, \
last ? 0xff123456 : myColor(i))
set key noautotitle
set multiplot layout 1,2
set title "fixed linecolor"
plot 2*sin(2*x) lw 2, \
for [i=0:2] $Data u 1:2:(myColor(i)) index i w filledcurves lc rgb var lw 2
set title "variable line color with fully transparent lines"
plot 2*sin(2*x) lw 2, \
for [i=0:2] x1=y1=(last=0,NaN) $Data u 1:2:(myColorX(i)) index i \
w filledcurves lc rgb var lw 2
unset multiplot
### end of script
Result:

gnuplot: 3d scatter plot with circles

I am trying to do a 3d plot, where I want each point to be with my choice of color/shape/shade. The fact is that I want to use the colour palettes from here.
Lets say my data is like this --
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
and my gnuplot command looks like this --
set style fill transparent solid 0.75 noborder
set style circle radius screen 0.01
splot "data.out" with circles linecolor rgb "blue"
and I am getting this plot --
as you can see, there is no circle, every point is +.
How do I draw with circles ?
To draw with circles you need
splot "data.out" linecolor "blue" pointtype 6
To get circle try type 6 or 7. Size of circle can be changed by adding option pointsize 2 (number is value of size)

Resources