9

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.

Kelvin S
  • 341
  • 2
  • 4
  • 13
  • 2
    interpolate your data with a much finer grid! If you need help for that, please provide your data or example data. – Robert Seifert Jun 29 '17 at 05:29
  • 7
    PLEASE! you are a functional programmer. DO NOT POST TEXT AS IMAGE. How annoying would it be if we would post our answers as images, you imagine? – Ander Biguri Jun 29 '17 at 10:34

1 Answers1

17

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);
Loamsiada
  • 444
  • 3
  • 11
  • I am trying the code you posted above. There is an error "Error using .* Matrix dimensions must agree." Is it due to the 6x6 matrix nature of xrow, yrow? How to fix it? Thanks. – Kelvin S Jun 29 '17 at 07:32
  • I have no multiply operator within my code. What did you do? You should update your original question with additional code. – Loamsiada Jun 29 '17 at 07:35
  • I updated my code and also posted my data. To try your method I directly put your code in mine but got that error mentioned above. Any suggestion? Thank you. – Kelvin S Jun 29 '17 at 08:13
  • 1
    I missed a second min and max within the linspace command. Maybe this solves your error. But your data also reveals, that your grid is from an ngrid generator... see my Note 3. – Loamsiada Jun 29 '17 at 08:13
  • https://stackoverflow.com/questions/74142685/methods-for-smoothing-contour-lines could you help with this question? – dtn Oct 21 '22 at 04:45