0

Is it possible with gnuplot to perform an operation on data (adding/multiplying) from two data files to generate a heatmap with the result of the operation ?

Ex: I have two files each with 4 columns, where

  • Col1: X coordinate
  • Col2: Y coordiante
  • Col3: Value
  • Col4: Uncertainty

I want to multiply the columns 3 of each file.

I wondered if something similar exists/would work in gnuplot, like ...

splot 'first.dat' using 1:2:(v=$3), 'second.dat' using 1:2:(v*$3)

I have been able to do this with two columns from the same file

splot 'first.dat' using 1:2:($3*$4)
Community
  • 1
  • 1
SmartCH
  • 231
  • 2
  • 6
  • Yes, it is (somehow) possible. People here expect that you show your own attempts and your code how far you have gotten and asking for help with a specific coding problem. Additionally, you should show the structure of your data (e.g. some example lines) to avoid wild guessing. – theozh Nov 18 '19 at 19:00

2 Answers2

1

A very similar question has already been answered: gnuplot plot data from two files

In your case it will look like that:

splot "<paste first.dat second.dat" u 1:2:($3*$6)

Note that all columns from both files are present, therefore you have to "skip" the ones from the second file.

Eldrad
  • 733
  • 3
  • 15
  • Great ! So simple - many thanks. Probably my search was too narrow, so I didn't find the other post. – SmartCH Nov 19 '19 at 11:28
1

The OP apparently runs Linux or MacOS. @Eldrad's nice and short solution won't work with Windows. Of course, you can install additional programs like gnuwin, awk, etc...

A platform independent and gnuplot-only (bit more complicated) solution is the following. You load the files 1:1 into datablocks and print these datablocks into a new datablock by appending each line. Assumption is of course that the two files have the same number of lines.

Code:

### plot data from different files combined with mathematical operation
# platform independent gnuplot-only solution
reset session

Windows = GPVAL_SYSNAME[:7] eq "Windows" ? 1 : 0   # otherwise Linux or MacOS

FILE = "first.dat"
Data = "$Data1"
if (Windows) { load '< echo   '.Data.' ^<^<EOD  & type "'.FILE.'"' }
else         { load '< echo "\'.Data.'   <<EOD" & cat  "'.FILE.'"' }

FILE = "second.dat"
Data = "$Data2"
if (Windows) { load '< echo   '.Data.' ^<^<EOD  & type "'.FILE.'"' }
else         { load '< echo "\'.Data.'   <<EOD" & cat  "'.FILE.'"' }

set print $Data
    do for [i=1:|$Data1|] {
        print $Data1[i][1:strlen($Data1[i])-1]."\t".$Data2[i]
    }
set print

splot $Data u 1:2:($3*$6)
### end of code
theozh
  • 22,244
  • 5
  • 28
  • 72