Search code examples
matlabheatmapcolorbarcolormap

How to create a smooth heatmap for 1D?


I have the temperature values ​​for the variable Y for 8 node points in 1D. Now when I try to plot I get the temperature gradient as individual boxes as shown below. I dont want this. The code for achieving this result is shown below (heatmap 1D)

heatmap 1D

Y =
1.0e + 03 *
4.1962
3.5087
2.8783
2.3026
1.7775
1.2967
0.8516
0.4318

 figure (1)
 imagesc (Y)
 axis equal
 axis off
 colorbar;

heatmap 1D (smooth)

I need to get an smooth image like the one shown in second figure(heatmap 1D smooth) where the transition is smooth between the temperature gradients.

Any help will be appreciated!


Solution

  • With interpolation we can achieve this. Initially, we have 8 values. We want to interpolate these so we have for example 100 values instead of just 8. In case you are not familiar with interpolation, the initial values will still be in place and all values we add in between will work as a gradient between the values we already have.

    In our case, we are interpolating in only 1 dimension and can use interp1.

    Y = 1000 * [4.1962 3.5087 2.8783 2.3026 1.7775 1.2967 0.8516 0.4318]';
    Y = interp1(Y, linspace(1,8))';
    
    figure (2)
    imagesc (Y)
    axis ([-3 5 0 100])
    axis off
    colorbar
    

    The second argument of interp1 is linspace(1,8). The default behaviour of linspace is to create 100 evenly spaced values starting with the first argument and ending with the last. These are the x-values where we are interpolating. In this case we don't really have any x-values, but since interp1 thinks of all our Y-values as a function of x, it assumes that the indexes of Y are the x-values if we don't give it any further instructions.

    Finally, we change the axis to be more similar to the image you posted in the question and get the following.

    Before...
    enter image description here

    After...
    enter image description here