How to get a normal box plot graph we get in origin software which consist of box plot with data points scattered and normal distribution curve in MATLAB

16 vues (au cours des 30 derniers jours)
I am trying to plot a boxplot in matlab which should contain boxplot with data points in the right with normal distribution curve in matlab. Like the one above

Réponse acceptée

Voss
Voss le 27 Mar 2024
Maybe something like this. This fits distributions to the data, but if you have the distributions already you can plot them the same way.
% random normal data
data = randn(100,3);
% box plot and distribution colors
colors = [0 0.6 0; 0.8 0.8 0; 0.8 0 0];
% box plot width
w = 0.3;
[N,m] = size(data);
% make the box plot
h = boxplot(data,'Widths',w);
% shift box plots to the left by w/2
xd = arrayfun(@(obj)get(obj,'XData')-w/2,h,'UniformOutput',false);
set(h,{'XData'},xd(:))
% set box plot colors
for ii = 1:m
set(h(:,ii),'Color',colors(ii,:),'MarkerEdgeColor',colors(ii,:))
end
hold on
% fit and plot the distributions
[xmin,xmax] = bounds(data(:));
x = linspace(xmin,xmax);
for ii = 1:m
plot(ii+w/4+w/2*rand(N,1),data(:,ii),'.','Color',colors(ii,:))
pd = fitdist(data(:,ii),'Normal');
plot(pdf(pd,x)+ii,x,'Color',colors(ii,:))
end
% set axes xticks and xticklabels
set(gca(),'XTick',1:m,'XTickLabels',string(char('A'+(0:m-1).')))

Plus de réponses (2)

Aquatris
Aquatris le 27 Mar 2024
Déplacé(e) : Image Analyst le 27 Mar 2024
I dont think there is a builtin plot like this. You can create your own functions for it or modify the community provided ones such as this.

Malay Agarwal
Malay Agarwal le 28 Mar 2024
Hi Deepan,
I understand that you want to create a boxplot with the normal distribution and the datapoints for each category overlayed to the right of each box.
Please try the following code:
% Step 1: Generate Sample Data
rng(10); % For reproducibility
dataA = normrnd(20, 5, [100, 1]);
dataB = normrnd(30, 10, [100, 1]);
dataC = normrnd(40, 15, [100, 1]);
categories = [repmat({'A'}, 100, 1); repmat({'B'}, 100, 1); repmat({'C'}, 100, 1)];
values = [dataA; dataB; dataC];
% Define colors for each category
colors = {'magenta', 'green', 'blue'}; % Magenta for A, Green for B, Blue for C
% Step 2: Create Boxplot
boxplot(values, categories, 'Whisker', Inf);
hold on;
% Step 3: Plot Normal Distributions next to each boxplot
uniqueCats = unique(categories, 'stable');
for i = 1:length(uniqueCats)
category = uniqueCats{i};
subset = values(strcmp(categories, category));
% Creating a range of values for plotting the normal distribution
x = linspace(min(subset), max(subset), 100);
% Calculating the normal distribution with the same mean and std as the data
pd = fitdist(subset, 'Normal');
y = pdf(pd, x);
% Scaling the y-values to match the boxplot size
y = y / max(y) * 0.15; % 0.15 is the scaling factor, adjust as needed
% Plotting the normal distribution with specified color
plot(i + y, x, 'Color', colors{i});
% Step 4: Overlay scatter plot for the actual data points
% Adjust position of each data point to align with the distribution curve
% We scatter the points with a slight random offset horizontally to avoid overlap
rng(10); % For consistent scatter positions
scatter_x = normrnd(i, 0.03, size(subset)) + max(y); % Adjust scatter_x for alignment
% Scatter with specified color and transparency
scatter(scatter_x, subset, [], colors{i}, 'filled', 'MarkerFaceAlpha', 0.3);
end
title('Boxplot with Normal Distribution and Data Points');
hold off;
The code:
  • Uses the “boxplot” function to create the initial boxplot.
  • Uses “hold on” to ensure that all subsequent plots are on the same figure.
  • Loops over each category and fits a normal distribution to the category’s data using the “fitdist” function.
  • Creates a PDF from the normal distribution using the “pdf” function and plots it.
  • Creates a scatter plot for the category’s data using the “scatter” function and plots it.
Please refer to the following resources for more information:
Hope this helps!

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by