Search code examples
gnuplotcontour

gnuplot projecting contuer to the XZ and YZ plane


I have a dataset that has x,y,z columns. I have created surface plot using gnuplot and contour on top and bottom as see below. Here is the dataset: https://www.dropbox.com/s/8evj5da7yco1xmo/datx.dat?dl=0

Below is the plot from the data in the link.

enter image description here

I'm trying to create a contour plot like the one below with contours on the side, how do I accomplish this in gnuplot?

enter image description here

Here is the solution http://gnuplot-tricks.blogspot.com/2010/ but I'm unable to replicate it for my dataset.


Solution

  • Your linked solution uses a matrix as input data with row- and column-index as x,y coordinates and you have x,y,z data columns. You could write your z-values into a matrix, but then you will lose your x- and y-coordinates. You could also possibly add them as the first row and first column "headers", respectively (gnuplot calls this a non-uniform matrix, check help matrix nonuniform).

    However, gnuplot can also plot x,y,z data as surface plot easily, especially if it is a regular grid (which seems to be the case with your data). However, this data has to be separated by empty lines into iso-curves (as @Joce mentioned in the comments).

    The script below

    • inserts empty lines into your data (gnuplot-only workaround without external tools)
    • plots projection lines (what @Ethan already assumed) to the xz- and yz-plane
    • plots contour lines on the surface and on the base

    This was a bit of a struggle (with set pm3d, set contour, etc.) to get it work, but I think the result is what you were asking for. There is room for fine-tuning. If there are simpler ways to do it, please let me know.

    Script:

    ### plot surface with contours and projections
    reset session
    
    FILE = "SO70490999.dat"
    colX = 1
    colY = 2
    colZ = 3
    
    # 1. read file into datablock, 2. insert marker when both x- and y-values change, 3. get xmax and ymax
    set table $Temp
        x1 = y1 = xmax = ymax = NaN
        plot FILE u (x0=x1,x1=column(colX),y0=y1,y1=column(colY), \
             xmax!=xmax || x1>xmax ? xmax=x1:0, ymax!=ymax || y1>ymax ? ymax=y1:0, \
             sprintf("%g %g %g",x1,y1, column(colZ)).(x0!=x1 && y0!=y1 && x0==x0 ? " #" : '')) w table
    unset table
    # insert empty line before lines with marker
    set print $Data
        do for [i=1:|$Temp|] {
            if (strstrt($Temp[i],"#")>0) { print ""}
            print $Temp[i]
        }
    set print
    
    # plot contour lines into a datablock
    myContours = "-242.2, -242.5, -243, -244, -245, -246, -247"
    set cntrparam level discrete @myContours
    set contour both
    set table $Contours
        splot $Data u 1:2:3 w l
    unset table
    unset contour
    
    set view 55, 300
    set key noautotitle
    set xyplane relative 0
    
    splot for [i=0:99:5] $Data every  ::i::i u (xmax):2:3 w l, \
          for [i=0:99:5] ''    every :::i::i u 1:(ymax):3 w l, \
                         '' u 1:2:3 w pm3d, \
          for [i=1:words(myContours)] $Contours index i u 1:2:3 w l lc i, \
          for [i=1:words(myContours)] $Contours index i u 1:2:(-400) w l lc i, \
    ### end of script
    

    Result:

    enter image description here