You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
plot gaussian and standard deviation on my histogram
5 views (last 30 days)
Show older comments
Hi everyone,
I have this code and have plotted the histogram and now want to have gaussian and standard deviation.
can anyone help me please?
figure('Name','A3{1}.BMd_Img_o_part1')
edges= linspace(0,255,21);
histogram(image1, 'BinEdges',edges,'normalization','pdf')
Accepted Answer
Star Strider
on 8 Sep 2020
image1 = 42*randn(1,1000)+42*3; % Create Data
figure('Name','A3{1}.BMd_Img_o_part1')
edges= linspace(0,255,21);
[muHat,sgHat] = normfit(image1);
pdfplot = normpdf(edges, muHat, sgHat);
figure
histogram(image1, 'BinEdges',edges,'normalization','pdf')
hold on
plot(edges, pdfplot, '-r', 'LineWidth',2)
hold off
.
14 Comments
Fatemeh Shomal Zadeh
on 8 Sep 2020
Thanks so much for your help. Just one question, does this code has standard deviation as well?
Fatemeh Shomal Zadeh
on 8 Sep 2020
I am sorry, my plot looks like this how I can increase the hight of the gaussian peak to reach to my histogram figure?
Also, how can I add the standard deviation to my figure?
Thanks a lot
Star Strider
on 8 Sep 2020
As always, my pleasure!
Increasing the height would not be appropriate. Note that the plot overestimates some of the bins, and underestimates some of the others.
I am not certain what you intend by ’Also, how can I add the standard deviation to my figure?‘ One possibility would be to use a text object. Otherwise, it depends on what you want to do and how you want the standard deviation depicted. The standard deviation is the second normfit output, ‘sgHat’.
Fatemeh Shomal Zadeh
on 8 Sep 2020
Thanks a lot for your explanatin.
My image1 data is the 368x315x3 uint8.
I do not understand this line image1 = 42*randn(1,1000)+42*3.
Can you help me how I can consider my data?
Star Strider
on 8 Sep 2020
As always, my pleasure!
‘My image1 data is the 368x315x3 uint8.’
I do not have your data to work with, or the code you used to create ‘image1’.
‘I do not understand this line image1 = 42*randn(1,1000)+42*3.’
I created data to use to test my code. I always do this if the actual data are not provided (as they were not provided here).
Note that the full line is:
image1 = 42*randn(1,1000)+42*3; % Create Data
.
Fatemeh Shomal Zadeh
on 8 Sep 2020
This data was extracted from ultrasound Machine and as I have mentioned is 368x315x3 uint8 array.
Hope this information can help you and I would be gratfeul how I can plot the gaussian in this situation.
My code are listed as below;
imshow(Data_SWS_BMd_A3{1}.BMd_Img_o)
impixelinfo
xmin = 5;
xmax = 314;
ymin = 136;
ymax = 238;
imshow(Data_SWS_BMd_A3{1}.BMd_Img_o(ymin:ymax,xmin:xmax));
impixelinfo
image= Data_SWS_BMd_A3{1}.BMd_Img_o(ymin:ymax,xmin:xmax)
[n,m]=size(image)
id=fix(m/3)
image1=image(:,1:id);
image2=image(:,id+1:2*id);
image3=image(:,2*id+1:m);
figure
imshow(image1)
impixelinfo
figure
imshow(image2)
impixelinfo
figure
imshow(image3)
impixelinfo
figure('Name','A3{1}.BMd_Img_o_part1')
edges= linspace(0,255,21);
histogram(image1, 'BinEdges',edges,'normalization','pdf')
thanks a lot
Fatemeh Shomal Zadeh
on 8 Sep 2020
Please let me know if you need any more information. I do really need to solve my problem as I need to report tomorrow in my meeting.
Thanks so much
Star Strider
on 9 Sep 2020
It might be interesting to have the ‘image1’ vector you refer to here:
histogram(image1, 'BinEdges',edges,'normalization','pdf')
although havng it would not change anything I already wrote, so I doubt providing it would be worth the effort to attach it.
Fatemeh Shomal Zadeh
on 9 Sep 2020
Unfortunately, image1 is not a vector, it is an array. When I am converting it to the vector as listed below it show this figure(i have attached here) which is not the figure that I am looking for
A = im2double(image1(:));
edges= linspace(0,255,21);
[muHat,sgHat] = normfit(A);
pdfplot = normpdf(edges, muHat, sgHat);
figure('Name','A3{1}.BMd_Img_o_part1')
histogram(image1, 'BinEdges',edges,'normalization','pdf')
hold on
plot(edges, pdfplot, '-r', 'LineWidth',2)
hold off
Star Strider
on 9 Sep 2020
Note that the histogram function will take matrix arguments as well as vector arguments, although it will convert a matrix to a single-column vector for analysis.
I am lost as to what you are doing.
Fatemeh Shomal Zadeh
on 9 Sep 2020
I am sorry, maybe I was not clear enough in explaining my question.
Let me explain from the beginning. I have plotted this histogram (see attachment please) and now want to have a gaussian plot which has overlab with the histogram.
Data already are in a grayscale.
imshow(Data_SWS_BMd_A3{1}.BMd_Img_o)
impixelinfo
xmin = 5;
xmax = 314;
ymin = 136;
ymax = 238;
imshow(Data_SWS_BMd_A3{1}.BMd_Img_o(ymin:ymax,xmin:xmax));
impixelinfo
image= Data_SWS_BMd_A3{1}.BMd_Img_o(ymin:ymax,xmin:xmax)
[n,m]=size(image)
id=fix(m/3)
image1=image(:,1:id);
image2=image(:,id+1:2*id);
image3=image(:,2*id+1:m);
figure
imshow(image1)
impixelinfo
figure
imshow(image2)
impixelinfo
figure
imshow(image3)
impixelinfo
edges= linspace(0,255,21);
[muHat,sigmaHat] = normfit(image1);
pdfplot = normpdf(edges, muHat, sigmaHat);
figure('Name','A3{1}.BMd_Img_o_part1')
histogram(image1, 'BinEdges',edges,'normalization','pdf')
hold on
plot(edges, pdfplot, '-r', 'LineWidth',2)
hold off
I am receiving error like this which I do not know what it is;
Error in normfit
xc = x - repmat(muhat,[n 1]);
And if I convert the image1 to the vector double it shows the graph which is not right.
I know it is becoming complicated and thanks so much for your patience. If still it is not clear I do appologize for that.
Kind regards
Star Strider
on 9 Sep 2020
I do not understand what threw the error.
Please use the code I already wrote to plot the normal distribution on the histogram.
More Answers (1)
See Also
Categories
Find more on Histograms 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)