Gabriel Ha, MathWorks
Learn how to leverage simple MATLAB® functions to customize the appearance of a histogram. You’ll learn how to accomplish tasks like changing the bin size and displaying relative frequencies on the y-axis instead of absolute counts.
This video demonstrates how to leverage simple MATLAB functions to customize the appearance of a histogram. You’ll learn how to accomplish tasks like changing the bin size and displaying relative frequencies on the y-axis instead of absolute counts.
In the first section, we generate 10,000 random numbers of standard normal distribution. The histogram function helps us visualize this data using default settings.
We can implement some useful customizations by passing additional parameters to the function. If we want to group our data into larger buckets, we simply pass in an array that specifies the bin edges; in this case, we’ll get bins of width 0.5 from -4 to 4.
Furthermore, we can change the histogram to display relative frequencies instead of absolute counts. How much of this data is concentrated in this highest bin? Hard to tell, but if we run the function with these parameters, we quickly see the answer is about 20%.
Finally, to give us more control on how our histogram is visualized, we’ll convert the histogram into a bar graph. We simply replace “histogram” with “histcounts” to get the count in each bin, and the bin edges. Note that we only need to supply the “count” variable to the bar function to reproduce the shape of the histogram. If we care about the x-axis matching up exactly with our previous histogram, we can use this code.
Now that we’re working with a bar graph, we can quickly apply useful customizations. First, we’ll modify the y-axis ticks to display percentages, and adjust the count to match. And as with any good graph, we should add a title, and label the axes.
To learn more about histograms and other customizations for MATLAB graphs, check out the links in the description.
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: .Select web site
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.