Search code examples
gnuplot

GnuPlot splot function with 2d points


I basically want to draw 2d color surface (or contour plot) of rosenbrock function f(x,y) = (a-x)^2 + b * (y-x*x) ^2 and append some points (x,y) on this image.

Sample file with points looks as follows:

#x #y
15.00000 12.00000
8.00000 9.00000

The thing is, both graphs do not share the same coordinate system on output image:

coordinate systems do not overlap on each other

gnuplot code:

    #!/usr/bin/env gnuplot
    reset
    set terminal png size 700,700
    enhanced set output 'output.png'
    set tmargin screen 1
    set bmargin screen 0
    set border 0 back
    set size square
    xr=20
    yr=20
    set xrange [-xr:xr]
    set yrange [-yr:yr]
    unset key #disablegraph name 
    unset colorbox 
    set surface
     set multiplot
    set view map
    set cntrparam levels 10# contour tenderness
    set style data pm3d
    set pm3d
    set contour
    a=1 #rosenbrock parameter
    b=1 #rosenbrock parameter

#set isosamples 50 splot (a-x) * (a-x) + b * (y-x*x) * (y-x*x) # 2d rosenbrock unset view unset pm3d plot 'data.dat' pt 5, 'data.dat' using 1:2:($0+1) with labels offset 1 notitle


Solution

  • mixing 2d and surface plots with multiplot is usually a mess. I guess you probably don't need multiplot in this simple case. Maybe something like this is enough:

    set size square
    xr=20
    yr=20
    set xrange [-xr:xr]
    set yrange [-yr:yr]
    unset key 
    unset colorbox 
    set surface
    set pm3d map
    set contour
    set cntrparam levels 10# contour tenderness
    
    rosenbrock(x,y,a,b)= (a-x) * (a-x) + b * (y-x*x) * (y-x*x)
    
    splot rosenbrock(x,y,1,1) w pm3d, 'data.dat' u 1:2:0 w p pt 5, 'data.dat' using 1:2:(1):($0+1) with labels offset 1,1 notitle