Asked by Oscar Hartogensis
on 24 Jan 2011

I have a 2D weighting function of a certain area that gives 1 when summed over all columns and rows. I want to make a contour plot where the iso-lines represent the percentage cumulative contribution to the total. I have seen several questions on the web that refer to this exercise but found no answer. Can anyone help?

..

Below an example that illustrates what contour.m gives (which is not what I want):

% Construct Example Weighting Function

x1 = -3:.02:3; x2 = -3:.02:3;

[X1,X2] = meshgrid(x1,x2);

F = mvnpdf([X1(:) X2(:)],[0 0],[.25 .3; .3 1]);

F = reshape(F,length(x2),length(x1));

F = F./sum(sum(F));

..

% Make Contour Plot

[C,h] = contour(x1,x2,F,10);

set(gca,'xlim',[-1.5,1.5],'ylim',[-2.5,2.5]);

clabel(C,h,'labelspacing',500)

..

--> Rather then these standard contour lines I want the first iso-line to represent the first 10% contribution to the total, the next contour line the 20% contribution to the total etc.

Answer by Oscar Hartogensis
on 28 Jan 2011

Accepted Answer

I found a solution! It is not very elegant but it works fine. It makes use of the fact that my weighting function (F) expands outward from a single maximum value and they are closed lines. These attributes allows the cumulative weighting function to be determined by sorting and cumulatively summing F:

% Sort the weighting function and take the cumsum

[FF,I] = sort(F(:),1,'descend');

FFsum = cumsum(FF);

% Load the values of FFsum into YY according to the original order of F

% given by index I

YY = ones(length(FF),1);

YY(I) = FFsum;

% Reshape YY to have the orignal form of F

YYY = reshape(YY,size(F));

% Plot the contours of the cumulaitve weight

figure

pcolor(x1,x2,F)

shading interp

figure

contour_levels = [[0.1:0.1:0.9] 0.98];

contour_labels = [[0.1:0.2:0.9] 0.98];

[C,h] = contour(x1,x2,YYY,contour_levels);

clabel(C,h,contour_labels,'labelspacing',500)

Answer by Andrew Newell
on 26 Jan 2011

You seem to be trying to plot the multivariate normal cumulative distribution function. For that, just change mvnpdf to mvncdf. You don't need the normalization step. Try this:

[X1,X2] = meshgrid(x1,x2);

F = mvncdf([X1(:) X2(:)],[0 0],[.25 .3; .3 1]);

[C,h] = contour(x1,x2,F,10);

set(gca,'xlim',[-1.5,1.5],'ylim',[-2.5,2.5]);

clabel(C,h,'labelspacing',500)

Oscar Hartogensis
on 26 Jan 2011

@Andrew: Thanks for responding

..

I used the mvnpdf function just to produce an example spatial weighting field that looks similar to the real thing that is based on a physical model. So I don't actually use mvnpdf.

..

The real model and the example based on mvnpdf are similar in the sense that the contours expand outward from a single maximum value and they are closed lines. Rather then contours of the weighting values themselves I would like to have contours representing the cumulative contribution of the area within each contour line to the total weight. So first contour line at 10% contribution, second at 20% etc.

Sign in to comment.

Answer by Andrew Newell
on 27 Jan 2011

Sign in to comment.

Answer by John Moseley
on 27 Jun 2013

This is great Oscar. Thanks for posting.

John

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.