Interpolating in a heatmap using Gnuplot.jl

I have a matrix with dimensions 26 x 25 and am using gnuplot to create a heatmap. This is relatively simple by running @gp :- comb "w image pixels". However, I have two questions

  1. Is it possible to interpolate data so that it is more of a continuous, smoothed plot rather than tiles?
  2. Is it possible to change the color scheme?

@affans, you may want to use Interpolations.jl for smoothing and palette() for the color scheme. Example edited to include a contour:

using Gnuplot, Interpolations
n, m = 26, 31
x, y = 1:n, 1:m
fxy = [sin(u * v) for u = x, v = y]
@gsp x y fxy "w image notit" "set view map" "set auto fix" "set size ratio -1"
@gsp :- xlab = "x" ylab = "y" "set cblabel 'z'"
@gsp :- palette(:default) #choose gnuplot palette (:thermal, etc)
save(term="pngcairo size 640,480", output="matrix.png")

cubint = CubicSplineInterpolation((x,y),fxy)
xi, yi = 1.0:0.2:n, 1.0:0.2:m
fxyi = [cubint(u,v) for u = xi, v = yi]
@gsp xi yi fxyi "w pm3d notitle ls 1"  "set view map" "set auto fix" "set size ratio -1"
@gsp :- xlab = "x" ylab = "y" "set cblabel 'z'"
@gsp :- palette(:default) #choose gnuplot palette (:thermal, etc)
@gsp :- "set style line 1 lw 1"  # contour width = 1
@gsp :- "set linetype 2 lc rgb 'yellow'"  # contour will be linetype 2
@gsp :- "set contour" "set cntrparam levels discrete -0.7"  # contour at -0.7
save(term="pngcairo size 640,480", output="matrix_interpolated_with_contour.png")

For a full list of color schemes:

for x in palette_names()
	println(x)
end

Cubic interpolation result:
matrix_interpolated

Original matrix:
matrix

Cubic interpolation plus one contour at -0.7 level:
matrix_interpolated_with_contour

2 Likes

Thanks, I have my own solution with pm3d and @gsp but I like your solution better. The only thing is if its possible to add a contour line for f(x, y) = C for some constant C?

Some of the gnuplot solutions posted on the web (1, 2) for this problem seem to plot the contours to a “table” first. Maybe you can dig that path further.

Thanks, I have definitely googled quite a few topics for contour lines. Most of them use pm3d with splot (i.e. @gsp) and since I am brand new to gnuplot (switching over from ggplot2), it’s a bit difficult to write the Julia code equivalent. However, I will try and post my results here for others to see.

1 Like

Translating the splot commands from Google searches, I have the following code:


    @gp :- "set view map"  # bird eye view of 3d plot
    @gp :- "set dgrid3d 100,100,100" ## edit these numbers to get more "resolution"
    @gp :- "set pm3d interpolate 0,0" ## edit/comment this line to see how things work. 
    @gp :- "set grid x y lc 'black' front"
    @gp :- "set palette rgbformulae 33,13,10"
    @gp :- "set xrange [1:24]"
    @gp :- "set xtics 1,1,24" "set ytics 1,1,31"
    #@gp :- "set cblabel 'Legend' norotate offset -10.7, 12.3"
    @gsp :- x y fxy "w pm3d notitle ls 1" 

which gives the plot

You can use the code about to manually do interpolation OR play around/read the docs for "set pm3d interpolate 0,0" for gnuplot to interpolate.

Now if you want to add contour lines, things are a bit tricky.

 @gp :- "set style increment user"
    @gp :- "set style line 1 lw 3"    # the width of the contour line uses line style 1 by default
    @gp :- "set style line 2  lc rgb 'black' " # contour lines uses line style 2 by default. 

    @gp :- "set contour"
    #@gp :- "set cntrparam cubicspline"  # Smooth out the lines"
    #@gp :- "set cntrparam levels incremental  0,100,2000"
    #@gp :- "set cntrlabel onecolor"
    @gp :- "set cntrparam levels discrete 0.8"  # Plot the selected contours
    #@gp :- "set cntrlabel start 5 interval 20"
    #set cntrlabel  format '%5.3g' font ',7'
 @gp :- "set view map"  # bird eye view of 3d plot
    @gp :- "set dgrid3d 100,100,100" ## edit these numbers to get more "resolution"
    @gp :- "set pm3d interpolate 0,0" ## edit/comment this line to see how things work. 
    @gp :- "set grid x y lc 'black' front"
    @gp :- "set palette rgbformulae 33,13,10"
    @gp :- "set xrange [1:24]"
    @gp :- "set xtics 1,1,24" "set ytics 1,1,31"
    #@gp :- "set cblabel 'Legend' norotate offset -10.7, 12.3"
    @gsp :- x y fxy "w pm3d notitle ls 1" 

The figure looks like

Notice the two line styles. The first line style actually edits the line width of the contours while the second line style gives the color. Also play around with the commented lines.

I am very new to Gnuplot so I can’t explain what each line does, these figures are basically a mashup of commands I found on the internet.

@affans, please check edited code in my reply above.

1 Like