Search code examples
matlabmatlab-figure

Combining two images in matlab (contourf changes the range of the plot)


I wrote below code to show a simple vector field and it's magnitude. I want to combine two images but the first one is plotted in an interval which I didn't want and when I type hold on, the second image is shown in a very small region of the first image! But I want them to be shown in the same regions. Could anyone help?

clear all;
close all;
[x,y] = meshgrid(-3:0.1:3,-3:0.1:3);
Idontwantthis = (x.^2+y.^2)<1;
data= sqrt((x./(x.^2+y.^2).^(3/2)).^2+(y./(x.^2+y.^2).^(3/2)).^2);
data(Idontwantthis)=NaN;
contourf(data,20);
u = (10000*x./(x.^2+y.^2).^(3/2));
v = (10000*y./(x.^2+y.^2).^(3/2));

figure
quiver(x,y,u,v)

The code is from here


Solution

  • You are not mentioning the locations while using contourf, so it is getting plotted w.r.t to indices. You have to mention the locations, so that you can use hold on.

    [x,y] = meshgrid(-3:0.1:3,-3:0.1:3);
    Idontwantthis = (x.^2+y.^2)<1;
    data= sqrt((x./(x.^2+y.^2).^(3/2)).^2+(y./(x.^2+y.^2).^(3/2)).^2);
    data(Idontwantthis)=NaN;
    contourf(x,y,data,20);
    u = (10000*x./(x.^2+y.^2).^(3/2));
    v = (10000*y./(x.^2+y.^2).^(3/2));
    
    u(Idontwantthis)=NaN;
    v(Idontwantthis)=NaN;
    % figure
    hold on
    quiver(x,y,u,v)