Clear Filters
Clear Filters

How to overlay box plot with distribution histogram in the same graph

16 views (last 30 days)
I want to overlay box plot on my distribution histogram (something like as shown in the figure). How can I do that?

Answers (1)

Jaswanth
Jaswanth on 26 Jul 2024 at 10:14
Hi Anchal,
To overlay a box plot on a histogram in MATLAB, you can use combination of histogram and boxplot functions.
Kindly go through the following step-by-step explanation of how to overlay box plot on distribution histogram:
Create the histogram of your data.
data = randn(1000, 1); % Normally distributed data
histogram(data, 'Normalization', 'pdf');
hold on;
Next is to overlay the box plot on the histogram. The boxplot function plots vertically by default, so we need to adjust it to align with the x-axis of the histogram.
h = boxplot(data, 'Orientation', 'horizontal', 'Positions', 0.1, 'Widths', 0.05, 'Colors', 'k');
set(h, 'LineWidth', 1.5); % Adjust line width for better visibility
In the example discussed above, the histogram is created with normalized data, and the box plot is oriented horizontally to align with the histogram. The position and width parameters ensure the box plot is placed correctly. Adjust these parameters as needed for your specific data.
To plot multiple histograms together in a single figure as shown in the reference image you have shared, you can consider using subplot function.
I hope the information provided above is helpful.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!