Gnuplot: add infinity value to colorbox - gnuplot

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)

Related

How to plot energy diagram using gnuplot

I have data file:
0 0 3 -0.17 6 -0.05
0 0 3 -0.23 6 0.90
0 0 3 -0.41 6 0.50
0 0 3 -0.50 6 -0.33
0 0 3 -0.20 6 0.80
I want to plot the figure like this which connects each point in the lines. Can you tell me how?
The following suggestion uses the plotting styles with boxxyerror, with vectors and with labels.
The zero level gets the color of the last entry because everything is plotted on top of each other. Check the following example as starting point for further tweaking.
Script:
### energy diagram
reset session
$Data <<EOD
0 0 3 -0.17 6 -0.05 A_{one}
0 0 3 -0.23 6 0.90 B_{two}
0 0 3 -0.41 6 0.50 C_{three}
0 0 3 -0.50 6 -0.33 D_{four}
0 0 3 -0.20 6 0.80 E_{five}
EOD
set key noautotitle
set xrange [-1:8]
myWidth = 0.3
plot for [i=1:3] $Data u (column(2*i-1)):(column(i*2)):(myWidth):(0):0:xtic(i*2-1) w boxxy lw 3 lc var, \
for [i=1:2] '' u (column(2*i-1)+myWidth):(column(i*2)): \
(column(2*i+1)-column(2*i-1)-2*myWidth):(column(i*2+2)-column(i*2)):0 w vec nohead lw 1 dt 3 lc var, \
'' u 5:6:7:0 w labels offset 6,0 tc var font ",16"
### end of script
Result:
Addition:
From your comments: If the levels are to close such that the labels overlap, you could add an individual offset (here in column 8).
Check the following example where the data is modified that the levels B and E are very close.
Script:
Edit after OP's comment: simplifications, with xerrorbar instead of with boxxyerror and with custom xtics
Edit 2: simplified input data
reduce the input data to the minimum
add xticlabel from columnheader
use column number as x-coordinate. Mind the difference in ...$Data u (col):col:...: e.g. if col=1, (col) is the fixed value of 1, and col is the value from column 1.
### energy diagram with individual offset of labels
reset session
$Data <<EOD
A B C Label Offset
0.0 -0.17 -0.05 A_{one} 0
0.0 -0.23 0.90 B_{two} 0.05
0.0 -0.41 0.50 C_{three} 0
0.0 -0.50 -0.33 D_{four} 0
0.0 -0.20 0.85 E_{five} -0.05
EOD
set key noautotitle
set errorbars 0
set offset 0.5,0.5,0,0
myWidth = 0.1
plot for [col=1:3] $Data u (col):col:(myWidth):0:xtic(columnhead(col)) w xerr lw 3 ps 0 lc var, \
for [col=1:2] '' u (col+myWidth):col:(1-2*myWidth):(column(col+1)-column(col)):0 \
w vec nohead lw 1 dt 3 lc var, \
'' u (3+myWidth):($3+$5):4:0 w labels left offset 1,0 tc var font ",16"
### end of script
Result:

Gnuplot stats min does not detect negative numbers

As you can see in this post gnuplot "stats" command unexpected min & "out of range" results, gnuplot stats take as the minimum value the minimum non-negative number avalaible in the column.
How can I include the negative numbers? My column has negative numbers and I want to have a negative number as a minimum for applying to the axis range.
Btw, I'm calling:
stats mytextfile u 2:3 nooutput
But in fact later I have to call it again because I want to have the min and max values from 4 columns. Can I do it at once? Or do I have to do as I'm doing the following?:
stats mytextfile u 2:3 nooutput
do whatever
stats mytextfile u 4:5 nooutput
do whatever
As mentioned in the question and answer you linked to, stats will be done on the current x- and y-ranges.
So, if you are in doubt just set the range [*:*], or [*:*][*:*] if you are using two columns.
Depending on what exactly you want to do you could use this as a starting point.
Code:
### stats on several columns
reset session
$Data <<EOD
1 -0.1 -0.2 -0.3 -0.4
2 0.0 0.0 0.0 0.0
3 0.1 0.1 0.1 0.1
4 0.2 0.2 0.2 0.2
5 0.3 0.3 0.3 0.3
6 0.4 0.4 0.4 0.4
7 1.5 2.5 3.5 4.5
EOD
do for [i=2:5] {
stats [*:*] $Data using i nooutput
print sprintf("Column %d: min: % 4g, max: % 4g",i, STATS_min, STATS_max)
}
### end of code
Result:
Column 2: min: -0.1, max: 1.5
Column 3: min: -0.2, max: 2.5
Column 4: min: -0.3, max: 3.5
Column 5: min: -0.4, max: 4.5
For the main question, put your range:
stats [-9999:9999] mytextfile u 2:3 nooutput

2D plots from several input data files

My code is returning 1000 snapshot_XXXX.dat files (XXXX = 0001, 0002,...). They are two columns data files that take a picture of the system I am running at a specific time. I would like to mix them in the order they are created to build a 2D plot (or heatmap) that will show the evolution of the quantity I am following over time.
How can I do this using gnuplot?
Assuming you want the time axis going from bottom to top, you could try the following:
n=4 # Number of snapshots
set palette defined (0 "white", 1 "red")
unset key
set style fill solid
set ylabel "Snapshot/Time"
set yrange [0.5:n+0.5]
set ytics 1
# This functions gives the name of the snapshot file
snapshot(i) = sprintf("snapshot_%04d.dat", i)
# Plot all snapshot files.
# - "with boxes" fakes the heat map
# - "linecolor palette" takes the third column in the "using"
# instruction which is the second column in the datafiles
# Plot from top to bottom because each boxplot overlays the previous ones.
plot for [i=1:n] snapshot(n+1-i) using 1:(n+1.5-i):2 with boxes linecolor palette
This example data
snapshot_0001.dat snapshot_0002.dat snapshot_0003.dat snapshot_0004.dat
1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0
1.5 0.0 1.5 0.0 1.5 0.0 1.5 0.0
2.0 0.5 2.0 0.7 2.0 0.7 2.0 0.7
2.5 1.0 2.5 1.5 2.5 1.5 2.5 1.5
3.0 0.5 3.0 0.7 3.0 1.1 3.0 1.5
3.5 0.0 3.5 0.0 3.5 0.7 3.5 1.1
4.0 0.0 4.0 0.0 4.0 0.0 4.0 0.7
4.5 0.0 4.5 0.0 4.5 0.0 4.5 0.0
5.0 0.0 5.0 0.0 5.0 0.0 5.0 0.0
results in this image (tested with Gnuplot 5.0):
You can change the order of the plots if you want to go from top to bottom. If you want to go from left to right, maybe this can help (not tested).

Heatmap with Gnuplot on a non-uniform grid

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)

gnuplot: yerrorbars with linecolor variable

I want to draw yerrorbars with different colors. I am able to draw points with different colors using the following code:
reset
plot "-" using 1:2:3 with points linecolor variable
# x y linecolor
-4.0 -3.8 1
-3.0 -2.9 1
-2.0 -2.1 2
-1.0 -1.2 1
1.0 1.1 1
2.0 2.2 2
3.0 3.3 3
4.0 4.5 3
end
But I am not sure how to extend this to yerrrorbars. When I try and use the following code, the errorbars are colored only with default color. How do I color the errorbars with a specific color?
reset
plot "-" using 1:2:($1-$2) with yerrorbars linecolor variable
# x y linecolor
-4.0 -3.8 1
-3.0 -2.9 1
-2.0 -2.1 2
-1.0 -1.2 1
1.0 1.1 1
2.0 2.2 2
3.0 3.3 3
4.0 4.5 3
end
I found a way to do this by separating the data and then plotting it. But if there is a way without separating the data it would be a nicer solution.
reset
plot "-" using 1:2:($1-$2) with yerrorbars lc 1, \
"-" using 1:2:($1-$2) with yerrorbars lc 2, \
"-" using 1:2:($1-$2) with yerrorbars lc 3
# x y
-4.0 -3.8
-3.0 -2.9
-1.0 -1.2
1.0 1.1
end
-2.0 -2.1
2.0 2.2
end
3.0 3.3
4.0 4.5
end
using specifies which columns will be the input for the command. So since your third column is linecolor, and yerrorbars linecolor expects the fourth column to be the line color, you need to specify using 1:2:($1-$2):3. So, this is the corrected version of your example:
reset
plot "-" using 1:2:($1-$2):3 with yerrorbars linecolor variable
# x y linecolor
-4.0 -3.8 1
-3.0 -2.9 1
-2.0 -2.1 2
-1.0 -1.2 1
1.0 1.1 1
2.0 2.2 2
3.0 3.3 3
4.0 4.5 3
end
The problem is, that the third column ($1 - $2) is used to plot the yerrorbar (the ydelta more specifically). The documentation:
3 columns: x y ydelta
You'll need to add another column for the linecolor. If you want to make up something fancy, you could do something like:
plot "/tmp/test.foo" using 1:2:($1-$2):(int($1)+1) with yerrorbars linecolor variable
(e.g. use the integer part of the first column and add 1).
Or you can also use ternary operators if you want to choose between two colors:
plot "-" using 1:2:($1 > 1 ? 1 : 3) with yerrorbars linecolor variable
(e.g. choose linecolor 1 if the value in the first column is greater than 1, linecolor 3 otherwise)

Resources