Search code examples
plotoctavevisualization

How to plot data over a non-rectangular region in Octave?


I have three arrays of equal size: x, y, z. I want to plot z over x, y. Problems is, those x and y do not represent a rectangular region, such as what would be in case of using meshgrid function.

I know I can use something like scatter, but that would graphically only give me the points themselves. What I want is the filled, smoothed picture. So as opposed to this created by scatter:

enter image description here

I would like something like this:

enter image description here

Any suggestion how this can be done? I have a feeling the data must be smoothed out somehow via interpolation or something else prior to plotting which itself should be simple.


Solution

  • You can use griddata() to interpolate your x,y data on a regular grid and then you can use imagesc() to plot the result.

    Here is a minimal example with a basic circle:

    % INPUT
    x = cos(0:0.1:2*pi);
    y = sin(0:0.1:2*pi);
    z = (0:0.1:2*pi);
    
    % Create a regular grid that have the same boundary as your x,y data
    [xx,yy] = meshgrid(linspace(-1,1,100),linspace(-1,1,100));
    % Grid interpolation 
    zz = griddata (x, y, z, xx, yy);
    % Plot
    imagesc(zz)
    colormap ([jet(); 1 1 1]);  % I add a last [1 1 1] triplet to set the NaN color to white.
    

    enter image description here

    Noticed that this will only works if you keep the default interpolation method (which is a linear interpolation). The other method (cubic and nearest) will extend the domain of definition by analytic continuation.