Search code examples
matlabmatlab-figure

plot the distribution in Matlab


I want to plot the distribution of the following data. But, I don't want the histogram of it. I have attached a figure. I only one the red one. Could you please help me? thanks

enter image description here

Finally I want a figure like this:

enter image description here

Hsamp=normrnd(0,5,100)
g = histogram(Hsamp)

Solution

  • To plot the histogram as a line you can retrieve the bin counts and bin edges by dot indexing the property from the histogram object, g:

    Bin Counts → g.BinCounts
    Bin Edges → g.BinEdges
    Bin Width → g.BinWidth

    To find the bin centres we can take all the edges start from the seconds edge and subtract half the bin width. After retrieving the bin centres and bin counts the plot() function can be used to plot the values against each other to produce a curve/line graph. An alternative can be to use the histcounts() function but the process will be pretty similar. All the properties in this histogram object can be seen in: MATLAB Documentation: Histogram Plot.

    Plotting Histogram Bars and Curves

    Hsamp = normrnd(0,5,100);
    subplot(1,2,1); g = histogram(Hsamp);
    xlabel("Bin Centres"); ylabel("Bin Counts");
    Bin_Counts = g.BinCounts;
    Bin_Width = g.BinWidth;
    Bin_Centres = g.BinEdges(2:end) - Bin_Width;
    
    subplot(1,2,2); plot(Bin_Centres,Bin_Counts);
    xlabel("Bin Centres"); ylabel("Bin Counts");
    

    Extension: Binning Fundamentals

    Binning Fundamentals Diagram