Matlab how to make smooth contour plot?

Kelvin S picture Kelvin S · Jun 29, 2017 · Viewed 17.6k times · Source

I want to represent data with 2 variables in 2D format. The value is represented by color and the 2 variables as the 2 axis. I am using the contourf function to plot my data:

clc; clear;

load('dataM.mat')

cMap=jet(256); %set the colomap using the "jet" scale
F2=figure(1);
[c,h]=contourf(xrow,ycol,BDmatrix,50);
set(h, 'edgecolor','none');

xlim([0.0352 0.3872]);
ylim([0.0352 0.3872]);

colormap(cMap);
cb=colorbar;
caxis([0.7 0.96]);
% box on;
hold on;

Both xrow and ycol are 6x6 matrices representing the coordinates. BDmatrix is the 6x6 matrix representing the corresponding data. However, what I get is this:

enter image description here

The following is the xrow and yrow matices:

enter image description here

The following is the BDmatrix matices:

enter image description here

Would it be possible for the contour color to vary smoothly rather than appearing as straight lines joining the data points? The problem of this figure is the coarse-granularity which is not appealing. I have tried to replace contourf with imagec but it seems not working. I am using MATLAB R2015b.

Answer

Loamsiada picture Loamsiada · Jun 29, 2017

You can interpolate your data.

newpoints = 100;
[xq,yq] = meshgrid(...
            linspace(min(min(xrow,[],2)),max(max(xrow,[],2)),newpoints ),...
            linspace(min(min(ycol,[],1)),max(max(ycol,[],1)),newpoints )...
          );
BDmatrixq = interp2(xrow,ycol,BDmatrix,xq,yq,'cubic');
[c,h]=contourf(xq,yq,BDmatrixq);

Choose the "smoothness" of the new plot via the parameter newpoints.

Plot sample

To reduce the Color edges, you can increase the number of value-steps. By default this is 10. The following code increases the number of value-steps to 50:

 [c,h]=contourf(xq,yq,BDmatrixq,50);

Sample fine

A 3D-surf plot would be more suitable for very smooth color-shading. Just rotate it to a top-down view. The surf plot is also much faster than the contour plot with a lot of value-steps.

 f = figure;
 ax = axes('Parent',f);
 h = surf(xq,yq,BDmatrixq,'Parent',ax);
 set(h, 'edgecolor','none');
 view(ax,[0,90]);
 colormap(Jet);
 colorbar;

Sample smooth

Note 1: Cubic interpolation is not shape-preserving. That means, the interpolated shape can have maxima which are greater than the maximum values of the original BDmatrix (and minima which are less). If BDmatrix has noisy values, the interpolation might be bad.

Note 2: If you generated xrow and yrow by yourself (and know the limits), than you do not need that min-max-extraction what I did.

Note 3: After adding screenshots of your data matrices to your original posting, one can see, that xrow and ycol come from an ndgrid generator. So we also must use this here in order to be consistent. Since interp2 needs meshgrid we have to switch to griddedInterpolant:

[xq,yq] = ndgrid(...
            linspace(min(min(xrow,[],1)),max(max(xrow,[],1)),newpoints ),...
            linspace(min(min(ycol,[],2)),max(max(ycol,[],2)),newpoints )...
          );
F = griddedInterpolant(xrow,ycol,BDmatrix,'cubic');
BDmatrixq = F(xq,yq);