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

조회 수: 8 (최근 30일)
Anchal Chandra
Anchal Chandra 2018년 7월 21일
답변: Jaswanth 2024년 7월 26일
I want to overlay box plot on my distribution histogram (something like as shown in the figure). How can I do that?

답변 (1개)

Jaswanth
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.

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by