How to overlay box plot with distribution histogram in the same graph
10 次查看(过去 30 天)
显示 更早的评论
I want to overlay box plot on my distribution histogram (something like as shown in the figure). How can I do that?
0 个评论
回答(1 个)
Jaswanth
2024-7-26
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!