2D plots from several input data files - gnuplot

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).

Related

How to plot grouped boxplot by gnuplot

I wonder how to use gnuplot to plot this figure:
There are two problems I have:
the ytic is ..., 10^2, 10^1, 10^2, 10^3, ... How to handle such a
case?
I know gnuplot support boxplot, but how to regroup boxplot
according to some label?
Since I don't have the original data for the figure, I make up some data by myself.
There are two companies A, B, and C, selling different fruits with four prices.
Apple prices of company A: 1.2 1.3 1.4 1.1
Banana prices of company A: 2.2 2.1 2.4 2.5
Orange prices of company A: 3.1 3.3 3.4 3.5
Apple prices of company B: 1.2 1.3 1.4 1.1
Banana prices of company B: 2.2 2.1 2.4 2.5
Orange prices of company B: 3.1 3.3 3.4 3.5
Apple prices of company C: 2.2 1.3 1.4 2.1
Banana prices of company C: 3.2 3.1 3.4 2.5
Orange prices of company C: 2.1 3.3 1.4 2.5
I wonder how to plot those numbers by gnuplot.
Your question is not very detailed and your own coding attempt is missing, hence, there is a lot of uncertainty. I guess there is no simple single command to get your grouped boxplots.
There are for sure several ways to realize your graph, e.g. with multiplot.
The assumption for the example below is that all files have the data organized in columns and equal number of columns and same fruits in the same order. Otherwise the code must be adapted. It all depends on the degree of "automation" you would like to have. Vertical separation lines can be drawn via headless arrows (check help arrow).
So, see the following example as a starting point.
Data:
'Company A.dat'
Apples Bananas Oranges
1.2 2.2 3.1
1.3 2.1 3.3
1.4 2.4 3.4
1.1 2.5 3.5
'Company B.dat'
Apples Bananas Oranges
1.2 2.2 3.1
1.3 2.1 3.3
1.4 2.4 3.4
1.1 2.5 3.5
'Company C.dat'
Apples Bananas Oranges
2.2 3.2 2.1
1.3 3.1 3.3
1.4 3.4 1.4
2.1 2.5 2.5
Code:
### grouped boxplots
reset session
FILES = 'A B C'
File(n) = sprintf("Company %s.dat",word(FILES,n))
myXtic(n) = sprintf("Company %s",word(FILES,n))
set xlabel "Fruit prices"
set ylabel "Price"
set yrange [0:5]
set grid y
set key noautotitle
set style fill solid 0.3
N = words(FILES) # number of files
COLS = 3 # number of columns in file
PosX = 0 # x-position of boxplot
plot for [n=1:N] for [COL=1:COLS] PosX=PosX+1 File(n) u (PosX):COL w boxplot lc COL, \
for [COL=1:COLS] File(1) u (NaN):COL w boxes lc COL ti columnhead, \
for [n=1:N] File(1) u ((n-1)*COLS+COLS/2+1):(NaN):xtic(myXtic(n))
### end of code
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

Define data at cell centers using VTK format

I would like to write a post-processor in order to open some flow field data in paraview (using vtk legacy format). I am fine with the mesh loading, but I have a question on the variables arrangement.
I need to put a value in every cell center and not in the cell nodes. Thus, I have one value for each cell and no way to have a value for each node. Do you know a way to fix this problem?
Thank you very much for your kind help
Sure, you can specify cell data in the legacy ASCII VTK file format. Here's a simple example of a rectilinear grid with two cell data arrays with vector elements:
# vtk DataFile Version 2.0
ASCII
DATASET RECTILINEAR_GRID
DIMENSIONS 4 2 2
X_COORDINATES 4 double
0.0 10.0 20.0 30.0
Y_COORDINATES 2 double
0.0 10.0
Z_COORDINATES 2 double
0.0 10.0
CELL_DATA 3
VECTORS first_array double
-1.0 0.0 0.0
0.0 1.0 0.0
1.0 0.0 0.0
VECTORS second_array double
-1.0 0.0 0.0
0.0 1.0 0.0
1.0 0.0 0.0

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)

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