Search code examples
matlabimage-processingprobability-density

how to set the height of histogram bar in matlab?


i am trying to get the histogram of binomial pdf function in matlab. i want y-axis to be in the pdf of binomial distribution and it should be between 0 and 1. i want to get a gradually decaying histogram from 0.6 pdf to 0 pdf. i want x-axis to be in number of occurence of the event and it should be between 0 and 5, as total number of samples is 5. i want the bar at x=5 approximately equal to zero which will imply that the probability of getting all 5 out of 5 events is approx. zero. i am doing the following in matlab;

p=0.1;
x=5;
n=2;
y=binopdf(n,x,p);
hist(y);

but the histogram i get has y-axis scaled from 0-300 and axis scaled 0-5 and only one bar appears in the histogram at x=0 with total height=300. can anyone tell me what am i doing wrong? thanks in advance!!


Solution

  • A histogram doesn't show the history of a data series. It does"binning" to show the number of occurrences of data.

    One problem might be the order of your arguments you wrote y=binopdf(n,x,p); but matlab is looking for Y = binopdf(X,N,P) notice n and x are switched.

    To get the results for x=0 to 5 showing the full progression I think you want something more like this

    p=0.1;
    x=0:1:5; %this is now a range of results from 0 to 5
    n=2;
    y=binopdf(x,n,p);
    figure(1)
    subplot(1,3,1);stem(x,y, 'filled'); grid on
    subplot(1,3,2);bar(x,y, 'BarWidth',1); grid on
    subplot(1,3,3);hist(y,6); grid on
    

    edit I modified the code a bit to show you some different graphing options. The middle one uses a bar graph, this might look better to you. The histogram is on the right. It shows the number of occurrences, not that pdf. it says there are 4 occurrences of 0, 1 occurrence of .18, and one occurrence of .81 enter image description here

    just for information, if you wanted to change the y axis of a figure use something like this

    y_min=0
    y_max=1
    plot(x,y)
    ylim([y_min, y_max])