How to Draw a Histogram in MATLAB
A histogram is a powerful graphical representation that shows the distribution of numerical data by grouping values into bins and displaying the frequency of observations within each bin. In MATLAB, drawing a histogram is straightforward thanks to the built-in histogram function, which offers extensive customization options. On top of that, whether you are analyzing experimental data, visualizing statistical properties, or creating publication-ready figures, mastering histograms in MATLAB is an essential skill. This article guides you through the complete process—from basic usage to advanced features—so you can create clear, informative histograms that effectively communicate your data’s story.
Prerequisites and Basic Setup
Before you begin, ensure you have MATLAB installed (R2014b or later is recommended, as the histogram function replaced the older hist function). You will need a dataset—either loaded from a file (e.Now, g. Day to day, , . csv, .Even so, txt, . mat) or generated within MATLAB. For this tutorial, we will use randomly generated data to illustrate examples Small thing, real impact..
% Generate 1000 random numbers from a normal distribution
data = randn(1000,1);
The randn function creates numbers drawn from a standard normal distribution (mean = 0, standard deviation = 1). This dataset will serve as our primary example Less friction, more output..
Drawing Your First Histogram
The simplest way to create a histogram in MATLAB is to call the histogram function directly with your data:
histogram(data);
This command automatically chooses the number of bins (default) and plots the histogram in the current figure window. The x‑axis represents the data values (binned intervals), and the y‑axis shows the count of observations in each bin. You can also specify the number of bins explicitly:
histogram(data, 30); % uses 30 bins
Alternatively, you can define bin edges:
edges = -3:0.5:3;
histogram(data, edges);
Here, edges is a vector that defines where each bin starts and ends. MATLAB will create bins from -3 to 3 with a width of 0.5.
Customizing the Appearance
A plain histogram may not be suitable for presentations or reports. MATLAB provides numerous properties to tweak colors, transparency, bin normalization, and more.
Changing Bin Colors and Edge Style
h = histogram(data, 20);
h.FaceColor = [0.2 0.6 0.8]; % RGB triplet for a soft blue
h.EdgeColor = [0 0 0]; % black edges
h.FaceAlpha = 0.7; % transparency (0 to 1)
You can also use predefined color names like 'red', 'green', or 'blue'.
Normalization Types
Instead of raw counts, you might want to display probability densities, percentages, or cumulative distributions. Use the Normalization property:
% Probability density (area under histogram = 1)
histogram(data, 20, 'Normalization', 'pdf');
% Percentage (sum of bar heights = 100)
histogram(data, 20, 'Normalization', 'percentage');
% Cumulative distribution
histogram(data, 20, 'Normalization', 'cdf');
Each normalization changes the interpretation of the y‑axis. Here's a good example: 'pdf' is useful when comparing histograms with different sample sizes, while 'cdf' shows the proportion of data less than or equal to a given value.
Adjusting Bin Width and Alignment
Use BinWidth to set a fixed bin width:
histogram(data, 'BinWidth', 0.3);
To center bins on integer values, use 'BinMethod' with options like 'integers':
histogram(data, 'BinMethod', 'integers');
Other automatic bin methods include 'sturges', 'scott', and 'fd' (Freedman‑Diaconis rule). MATLAB’s default is 'auto', which usually works well.
Adding Labels, Title, and Grid
A histogram is incomplete without proper labels and a title. Use standard MATLAB plotting commands:
histogram(data, 30, 'FaceColor', [0.8 0.2 0.2], 'EdgeColor', 'w');
xlabel('Data Value');
ylabel('Frequency');
title('Histogram of Random Normal Data');
grid on;
The grid on command adds a light grid to help readability. You can also adjust font sizes:
set(gca, 'FontSize', 12);
Handling Real Data from Files
Often your data is stored in an external file. Load it and then draw the histogram:
% Load from a CSV file (assuming numeric column)
data = csvread('measurements.csv');
histogram(data);
If your file has headers, use readtable and extract the numeric column:
T = readtable('measurements.csv');
values = T.VarName1; % replace with actual column name
histogram(values);
Always inspect your data for missing values (NaNs) and remove them if necessary:
values = rmmissing(values);
Overlaying Multiple Histograms
Comparing distributions of two or more datasets is common. You can overlay histograms using the hold on command and adjusting transparency:
data1 = randn(1000,1);
data2 = randn(1000,1) + 1; % shifted mean
histogram(data1, 30, 'Normalization', 'pdf', 'FaceColor', 'b', 'FaceAlpha', 0.5);
hold on;
histogram(data2, 30, 'Normalization', 'pdf', 'FaceColor', 'r', 'FaceAlpha', 0.5);
hold off;
legend('Dataset 1', 'Dataset 2');
xlabel('Value');
ylabel('Probability Density');
title('Overlay of Two Normal Distributions');
With transparency (FaceAlpha), overlapping bins remain visible. For clarity, consider using 'EdgeColor' for each histogram And it works..
Histogram vs. Bar Plot
A histogram is often confused with a bar plot, but they serve different purposes. In MATLAB, never use bar to visualize continuous data distributions unless you have already calculated bin counts manually. Here's the thing — a bar plot (bar) displays categorical data where each bar represents a distinct category. A histogram groups continuous numerical data into intervals (bins). The histogram function handles binning automatically and correctly It's one of those things that adds up..
Advanced: Fitting a Distribution Over the Histogram
To add a theoretical distribution curve (e.g., normal PDF) over your histogram, use the fitdist and pdf functions:
% Fit a normal distribution to the data
pd = fitdist(data, 'Normal');
% Generate x values for the curve
x = linspace(min(data), max(data), 100);
y = pdf(pd, x);
% Plot histogram and overlay curve
histogram(data, 30, 'Normalization', 'pdf', 'FaceAlpha', 0.6);
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
hold off;
legend('Data', 'Fitted Normal');
This approach helps you visually assess how well your data matches a known distribution Not complicated — just consistent..
FAQ
Q: What is the difference between hist and histogram in MATLAB?
A: hist is an older function that plots bar heights based on bin counts. histogram (introduced in R2014b) is recommended because it returns a Histogram object that gives you full control over properties and normalization. hist is still available but may be removed in future versions.
Q: How can I save the histogram as an image?
A: Use saveas(gcf, 'histogram.png') or exportgraphics(gcf, 'histogram.pdf', 'Resolution', 300) for high‑quality output.
Q: Why are my bins empty?
A: Empty bins occur when no data falls within that interval. If you manually set bin edges, ensure they cover your data range. Use xlim([min(data) max(data)]) to adjust the axis And it works..
Q: Can I create a histogram with logarithmic axes?
A: Yes, set set(gca, 'XScale', 'log') or 'YScale', 'log'. Even so, note that log‑scaled histograms require careful bin selection—linear bins on a log scale can be misleading Easy to understand, harder to ignore..
Conclusion
Drawing a histogram in MATLAB is an efficient way to explore and present the distribution of your data. Think about it: starting with a simple histogram(data) call, you can progressively customize bins, colors, normalization, and overlays to create clear, publication‑ready figures. Whether you are analyzing sensor readings, experimental results, or simulated data, the flexibility of MATLAB’s histogram function empowers you to visualize patterns and outliers effectively. Practice with real datasets, experiment with different bin methods, and soon you will be able to produce histograms that tell the story behind your numbers at a glance.
MATLAB's histogram function provides an efficient, precise tool for visualizing data distributions, enabling clear communication of patterns and trends. Its intuitive design enhances analytical clarity, making it a cornerstone for effective data interpretation and presentation That's the whole idea..