# Interpolating in a heatmap using Gnuplot.jl

**URL:** https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683
**Category:** General Usage
**Tags:** plotting, interpolations
**Created:** [December 11, 2020, 8:29pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683 "2020-12-11T20:29:24Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [December 11, 2020, 8:29pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/1 "2020-12-11T20:29:24Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 13, 2020, 11:34am UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/2 "2020-12-13T11:34:58Z")

</div>

@affans, you may want to use [Interpolations.jl](http://juliamath.github.io/Interpolations.jl/latest/) for smoothing and `palette()` for the color scheme. Example edited to include a contour:

```julia
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:

```julia
for x in palette_names()
	println(x)
end

```

Cubic interpolation result:  
 ![matrix_interpolated](https://global.discourse-cdn.com/julialang/original/3X/2/9/2999e1fa77702686f62a21b259345a8338bc52d3.png)

Original matrix:  
 ![matrix](https://global.discourse-cdn.com/julialang/original/3X/b/e/beca6c96ba56276e589d07b1ff4ecf7f7758aaf9.png)

Cubic interpolation plus one contour at -0.7 level:  
 ![matrix_interpolated_with_contour](https://global.discourse-cdn.com/julialang/original/3X/7/3/732bafcec2f06825dcd1acac016c397c81f0a89e.jpeg)

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [December 13, 2020, 6:59pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/3 "2020-12-13T18:59:33Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 13, 2020, 7:54pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/4 "2020-12-13T19:54:01Z")

</div>

Some of the gnuplot solutions posted on the web ([1](http://www.phyast.pitt.edu/~zov1/gnuplot/html/contour.html), [2](https://stackoverflow.com/questions/26284546/gnuplot-ovelap-contour-and-heat-map)) for this problem seem to plot the contours to a “table” first. Maybe you can dig that path further.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [December 13, 2020, 8:01pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/5 "2020-12-13T20:01:48Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [December 13, 2020, 9:43pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/6 "2020-12-13T21:43:12Z")

</div>

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

```julia

    @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

 ![test_1](https://global.discourse-cdn.com/julialang/original/3X/f/3/f33ff3096397a815658278d967b0af2ca9d7ae0e.png)

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.

```julia
 @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

 ![test_2](https://global.discourse-cdn.com/julialang/original/3X/9/5/9514884072d192aeb7a4c51dbcc0a7f75859769b.png)

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.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 15, 2020, 12:20pm UTC](https://discourse.julialang.org/t/interpolating-in-a-heatmap-using-gnuplot-jl/51683/7 "2020-12-15T12:20:18Z")

</div>

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