Search code examples
matlabmatlab-figure

countourf doesn't show colored regions in Matlab


What should I change in my code that countorf shows regions with more color differences?

clear all;
close all;
phi=90;
[xx,yy] = meshgrid(-20:0.1:20,-20:0.1:20);
Idontwantthis = (xx.^2+yy.^2)<0.1;
u = (xx./(xx.^2+yy.^2).^(3/2));
v = (yy./(xx.^2+yy.^2).^(3/2));
data= sqrt(u.^2+v.^2);
data(Idontwantthis)=NaN;
u(Idontwantthis)=NaN;
v(Idontwantthis)=NaN;
contourf(xx,yy,data,20);

Solution

  • First, you don't need meshgrid over [-20 20]. Try reducing the range to like [-2 2]. After you run contourf, change colormap:

    clear all;
    close all;
    phi=90;
    
    [xx,yy] = meshgrid(-2:0.01:2,-2:0.01:2);
    
    Idontwantthis = (xx.^2+yy.^2)<0.1;
    u = (xx./(xx.^2+yy.^2).^(3/2));
    v = (yy./(xx.^2+yy.^2).^(3/2));
    data= sqrt(u.^2+v.^2);
    data(Idontwantthis)=NaN;
    u(Idontwantthis)=NaN;
    v(Idontwantthis)=NaN;
    
    contourf(xx,yy,data,20);
    colormap hsv;
    

    This is what your result will look like: contour plot with hsv colormap

    Hope that helps!