plot histogram without using matlab hist() function

조회 수: 27 (최근 30일)
ws
ws 2011년 12월 22일
댓글: Saad Alzubaidi 2022년 9월 24일
Hi all
There is a function called hist(), but if I want to plot a graph without using hist() command, how to plot a graph? I means using command to do the job same as hist().
Thank you.
  댓글 수: 2
Aditi Shetty
Aditi Shetty 2018년 8월 19일
A=imread('<your image path>');
a=rgb2gray(A);
subplot(3,1,1);
imshow(a);
title('original image');
[r,c]=size(a);
z=zeros(1,256);
for i=1:r
for j=1:c
b=a(i,j);
z(b+1)=z(b+1)+1;
end
end
N=sum(z);
p=zeros(1,256);
s=zeros(1,256);
c=zeros(1,256);
r=zeros(1,256);
for k=1:256
p(k)=z(k)/N;
if k==1
c(k)=p(k);
s(k)=c(k)*255;
r(k)=floor(s(k));
else
c(k)=c(k-1)+p(k);
s(k)=c(k)*255;
r(k)=floor(s(k));
end
end
subplot(3,1,3)
stem(r,z)
title('histogram')
Ritvik Ramesh Palvankar
Ritvik Ramesh Palvankar 2019년 9월 10일
Hey Adil,
Can you explain me the code after "for k=1:256". I did not understand the part where you took s(k)=c(k)*255
Also, why is zero matrix used and why is c(k-1) added to p(k)

댓글을 달려면 로그인하십시오.

채택된 답변

Naz
Naz 2011년 12월 22일
It depends on how you want to bin your data and what kind of data you have.
The idea of hist function is similar to stacks of coins: Let's say you have a bag of coins and after you separate them by values and stack on one another, you will get different heights. The hist function does the same.
data=[1 3 5 7 4 8 0 1 3];
If you want to distribute your data into 10 bins, you would create a new array of size 10:
histArray=zeros(1,10); % prealocate
x=0:1:9;
then, you would run a forloop to count how many times you encounter in a particular value:
for n=1:length(data)
histArray(1,data(n)+1)=histArray(1,data(n)+1)+1; % every time you meet the particular value, you add 1 into to corresponding bin
end
bar(histArray)
There could be another case. Let's say you want to distribute your data into 5 bins only (0 and 1 go in the 1st bin, 2 and 3 go in the 2nd...). In this case, you would run the same loop but with more complicated conditioning:
histArray=zeros(1,5);  %prealocate
x=0:2:8 % create bin numbers for plot
for n=1:length(data)
histArray(1,floor(data(n)/2)+1)=histArray(1,floor(data(n)/2)+1)+1;
% every time you meet the particular value
%you add 1 into to corresponding bin
end
bar(x,histArray)
You can design your own hist function to fit you needs
  댓글 수: 3
Anna Kasdan
Anna Kasdan 2018년 10월 7일
Hi! I am trying to do this with data that includes negative numbers, and I get the error 'Subscript indices must either be real positive integers or logicals.' Any thoughts? The data I am working with is:
n = 200; a = 0; b = 2; data = b.* randn(1, n) + a;
Sahitya Bonumaddi
Sahitya Bonumaddi 2021년 9월 1일
i'm stuck here too

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Adrian Lukasik
Adrian Lukasik 2015년 5월 13일
편집: Adrian Lukasik 2015년 5월 13일
hello I have another problem. The same thing, but in 3D: There is a function called hist3(), but if I want to plot a graph without using hist3() command, how to plot a graph? I means using command to do the job same as hist3(). I have matrix b, 2x1000, and the task about making histogram plot without hist3() function.
Thank you.
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 5월 13일
You should open a new Question for that.
Once you have the counts for each grid-point, use bar3()

댓글을 달려면 로그인하십시오.


Saad Alzubaidi
Saad Alzubaidi 2022년 9월 24일
clear all,clc;
x=imread(imgetfile);
imtool(x)
for r=1:3
v=x(:,:,r);
h(1:255,3)=0;
for i=1:size(x,1)
for j=1:size(x,2)
h(v(i,j)+1,r)=h(v(i,j)+1,r)+1;
end
end
end
figure,plot(h);
  댓글 수: 1
Saad Alzubaidi
Saad Alzubaidi 2022년 9월 24일
This is the code you requested, for the color image.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by