Search code examples
matlabgraphsubtraction

Matlab_ Plot the difference between two graphs


I am new to Matlab and I am struggling plotting the difference between these two graphs (subtract one plot from another) ... Can anyone help me?

% 2D plot of original target locations
X= double(xCoords);
Y= double(yCoords);
originalvalues = hist3([X(:) Y(:)],[30 40]);
imagesc(originalvalues)
contourf(originalvalues)
c= colorbar;
c.Label.String = 'Initial location';
axis equal
axis xy
xlabel('endCoordinatesx');
ylabel('endCoordinatesy');
title('2D Map of Original locations');

% 2D plot of final target locations
Xf= Design.endCoordinatesX;
Yf= Design.endCoordinatesY;
values = hist3(double([Xf(:) Yf(:)],[30 40]));
imagesc(values)
contourf(values)
c= colorbar;
c.Label.String = 'Final location';
axis equal
axis xy
xlabel('endCoordinatesx');
ylabel('endCoordinatesy');
title('2D Map of final locations');

Solution

  • If I understood well your problem, you want to making a third plot representing the difference between the two datasets.

    What you have to do is to get common bins for all the histograms you calculate :

    % find common centers for the two datasets
    [~, centers] = hist3(cat(2,[X(:) ; Xf(:)],...
                               [Y(:) ; Yf(:)]),...
                         [30 40]);
    
    % then you can calculate the histogram for each set of data : 
    originalvalues = hist3([X(:)  Y(:) ], centers);
    values         = hist3([Xf(:) Yf(:)], centers);
    
    % finaly, compute the difference between the two (now the bins are "aligned")
    differenceValue = values - originalvalues;