SmartCH
SmartCH

Reputation: 231

Adding/multiplying heatmaps in gnuplot

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

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)

Upvotes: 0

Views: 131

Answers (2)

theozh
theozh

Reputation: 25694

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

Upvotes: 1

Eldrad
Eldrad

Reputation: 743

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.

Upvotes: 1

Related Questions