Clear Filters
Clear Filters

How to draw a contour plot over a bi-variate Gaussian in such a way that the lines represent the probability of a sample being within that line?

2 views (last 30 days)
I have fitted a Gaussian mixture distribution model to my data and made a contour plot as seen in the figure.
The problem is that I don't know what the contour lines represent. I want the distance from the mean to the lines to have a measure of the variance in that direction. In other words I want the lines to say for example: "there is 68% chance of being within the first line, 95% chance of being within the second line..." Can this be done?

Answers (2)

Kian Azami
Kian Azami on 12 Oct 2017
Edited: Kian Azami on 12 Oct 2017
By defining the annotations in your code and defining the position of the annotation as the position of your contour, you can show the percentage of each contour on your plot.
m1 = figure
p1 = plot(X_Data,Y_Data);
a1 = annotation(m1,'textbox',...
[X_origin Y_origin Width Height],...
You can generate the annotation for the first time and then by the command "get(a1)" you can find its position and dimension. Then by comparing to that you can again define a better way to adjust the position of your annotation.
If you have the data relevant to your contour you can make all of the passage automatically by the code, however, it needs a little bit more effort.
Kian Azami
Kian Azami on 12 Oct 2017
Edited: Kian Azami on 12 Oct 2017
These division are based on the standard deviation of your data. So the first standard deviation from the mean is about 68 percent, the second standard deviation is about 95 percent, and so on... you can calculate them based on the standard deviation. I put the photo which I searched and it will give you a better idea:
So as you see from -1 SD to 1 SD covers 68.2 % of your data and from -2 SD to 2 SD covers 95.4 % of your data, and from -3 SD to 3 SD covers 99.8 % of your data and so on ...
I think you need to find the integral of the portion of diagram between the standard deviation and divide it to the integral over the whole diagram.
You can find the values for these percentages from the table below (wikipedia) :

Sign in to comment.

Bjørnar Dalsnes
Bjørnar Dalsnes on 12 Oct 2017
To clarify my question: I use fitgmdist to get the Gaussian distribution. Then I draw a contour plot of the distribution using fcontour. The problem is that I don't understand at what interval these lines are drawn. I would like the lines to be drawn such that 68% of the samples are within the first line, 95% are within the second line and so on.
  1 Comment
Kian Azami
Kian Azami on 12 Oct 2017
For this I will do like this. I will draw a horizontal line from the center which is the mean of your data to the end. For example:
Y_mean = mean(y);
Y_Data = Y_mean*ones(1,X_Data);
Then you find the intersection of the Gaussian Contour and the line you created (X_Data,Y_Data). There is a very easy way to do that which I don't remember right now.
Then you can find the distance of each contour respect to the center.
The ratio of the distances are equivalent to the square root of the ratio of the areas so: (distanc_1/distance_total)^2 = area1/area_total = The_Percentage.
If it is equal to 68 percent you understand that you are on the 1st standard deviation. If not you will check the next contour distance to the center and do the same procedure.
Can be an idea to try!

Sign in to comment.


Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!