Cumulative Contribution Contour Plot

23 views (last 30 days)
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.

Accepted Answer

Oscar Hartogensis
Oscar Hartogensis on 28 Jan 2011
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)
  1 Comment
K E
K E on 15 Aug 2014
The inpolygon.m function might be helpful (true for points inside a polygon, which you could derive from the your contour line).

Sign in to comment.

More Answers (3)

Andrew Newell
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)
  1 Comment
Oscar Hartogensis
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.


Andrew Newell
Andrew Newell on 27 Jan 2011
Oscar, the real issue here is how to define the cdf for your function. Once you can calculate it, you can do a contour plot of it as you would any other function. The question that only you can answer is, how do you want to define that cdf? For example, mvncdf(X) calculates the probability that a random vector will fall within some semi-infinite rectangle with upper limits defined by X (see the documentation). To get the equivalent for your pdf, you'd need to be able to integrate it analytically or numerically (possibly using the Symbolic Toolbox or one of the Matlab quadrature functions). But there are other ways you could define it - for example, distance from the point where the maximum value occurs. It all depends on how you want to use this function.

John Moseley
John Moseley on 27 Jun 2013
This is great Oscar. Thanks for posting.
John

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!