Plotting histogram with increments

8 views (last 30 days)
I want to plot histogram. My data consists of (say 1000 numbers) from -12 to 12 and I want to plot a histogram of 26 bins from -12 to -11.9 then from -11.9 to -11, -11 to -10 and till 10 to 11 and 11 to 11.9 and 11.9 to 12. How do i do this?
Please give me some logic to do this?
  1 Comment
Shambhavi Adhikari
Shambhavi Adhikari on 4 Feb 2019
My X is an array with 110000 numbers from -12 to 12. And I want to plot histogram like this. Can you help me to put a simple logic for this as the code is so long writing like this.
h1 = histogram(x(x>=-12 & x <=-11.9));
hold on;
h1 = histogram(x(x>=-11.9 &x <=-11));
hold on;
h1 = histogram(x(x>=-11 & x <=-10));
hold on;
h1 = histogram(x(x>=-10 & xx <=-9));
hold on;
h1 = histogram(x(x>=-9 & x <=-8));
hold on;
h1 = histogram(x(x>=-8 & x <=-7));
hold on;
h1 = histogram(x(x>=-7& x <=-6));
hold on;
h1 = histogram(x(x>=-6 & x <=-5));
hold on;
h1 = histogram(x(x>=-5&x <=-4));
hold on;
h1 = histogram(x(x>=-4 & x <=-3));
hold on;
h1 = histogram(x(x>=-3 & x <=-2));
hold on;
h1 = histogram(x(x>=-2 & x <=-1));
hold on;
h1 = histogram(x(x>=-1 & x <=0));
hold on;
h1 = histogram(x(x>=0 & x <=1));
hold on;
h1 = histogram(x(x>=1& x <=2));
hold on;
h1 = histogram(x(x>=2 &x <=3));
hold on;
h1 = histogram(x(x>=3 &x <=4));
hold on;
h1 = histogram(x(x>=4& x <=5));
hold on;
h1 = histogram(gx(gx>=5 & x <=6));
hold on;
h1 = histogram(x(x>=6 & gx <=7));
hold on;
h1 = histogram(x(x>=7& x <=8));
hold on;
h1 = histogram(x(x>=9 & x <=10));
hold on;
h1 = histogram(x(x>=10 & x <=11));
hold on;
h1 = histogram(x(x>=11& x <=11.9));
hold on;
h1 = histogram(x(x>=11.9 & x <=12));
hold off;
axis([xmin xmax]);
legend({'-S','-11','-10,'-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'});
And my axis doesn't work. And y axis must be in percentage.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 4 Feb 2019
Edited: Star Strider on 4 Feb 2019
I am not certain what you are asking. You can easily specify the bin edges in the histogram (link) function.
From the documentation:
histogram(X,edges) sorts X into bins with the bin edges specified by the vector, edges. Each bin includes the left edge, but does not include the right edge, except for the last bin which includes both edges.’
EDIT —
Responding tto the Comment you posted below your Question, this should do what you want:
X = 24*rand(1,110000)-12; % Create Data
BinEdges = -12 : 12;
h = histogram(X, BinEdges, 'Normalization','probability');
XTL = {'-S','-11','-10','-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'};
set(gca, 'XTick',BinEdges', 'XTickLabel', XTL)
xlabel('Bin Ranges')
ylabel('Probability')
See the documentation on Histogram Properties (link) for a discussion of all the options.
  38 Comments
Shambhavi Adhikari
Shambhavi Adhikari on 7 Feb 2019
Hi,
Looks like the xfcn = @(pct) cumsum(BinValues)>=(0.5-pct/200) & cumsum(BinValues)<=(0.5+pct/200);
x80 = BinCenters(xfcn(80));
x90 = BinCenters(xfcn(90));
is not working. I am trying to get the x values of the data where most of the data frequency lies (90%). I have attached a figure. Most of my data lies in between 200 to 205 but, when I try to use the above command, it gives me value as 195.5 to 200.5. Can you help me on this?
example.PNG
Star Strider
Star Strider on 7 Feb 2019
Can you help me on this?
Unfortunately, I cannot. I do not have your data, so I cannot experiment with it. From the image you posted, it looks as though your data are between 195 and 210, and that you have only 3 bins.
My code is based on your having 24 bins, since that is what you previously wanted, and my code works as well as it can (and is as accurate as it can be given that bin precision) with those results. It calculates the approximate centre of your data (as 50% of the maximum, that may not be the mode of your data distribution), and uses the cumsum function and thresholding to choose the bins with the requested data frequency. The data I synthesised to test my code was from a normal distribution, so it was symmetrical.
I do not know the distribution of your data. I suggest that you use the histfit (link), fitdist (link), or similar function to determine the distribution that best fits your data. You can then determine from the returned parameters and the cdf (link) function for that distribution how best to estimate the probabilities you are interested in.

Sign in to comment.

More Answers (0)

Categories

Find more on Two y-axis 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!