Error bar on probability distribution function

Dear Matlab Community,
I have this set of data and I need to graph PDF and put error bars (std) on it. Would you please help me on this?
have written this for graphing PDF, but I do not know how to add error bar? or shaded error.
load ("diameter2.mat")
pd = fitdist(diameter_TAC','Kernel','Width',4);
x = 0:1:25;
y = pdf(pd,x);
z = plot(x,y,'k-','LineWidth',2);
Thank you !!

 Risposta accettata

Vinay
Vinay il 20 Set 2024
Modificato: Vinay il 20 Set 2024
Hi Neda,
The standard deviation represents the data spread around the mean and used to plot the error bar on the probability distribution plot by using the “errorbar” function. The standard deviation is used as the parameter for the errorbar height.
The code below shows the plotting of the error bars on the pdf plot:
data = diameter_TAC';
% Calculate the standard deviation of the data
std_dev = std(data);
% Plot the PDF
figure;
plot(x, y, 'k-', 'LineWidth', 2);
hold on;
% Add error bars
% plot them at each point of x.
errorbar(x, y, repmat(std_dev, size(x)), 'k.', 'LineWidth', 1);
Kindly refer to the below documentations of “repmat” and “errorbar” for more details:

4 Commenti

Dear Vinay,
Many thanks for your answer. My PDF graph is attached. But, in your code, it is different. Another question, how can I have vertical error bar?
Thanks
Hi Neda,
The issue arises due to very small value of the pdf as compared to the standard deviation of the data. We can scale the standard deviation to match the pdf values to visualize the graph.
% Sample data
data = diameter_TAC';
pd = fitdist(data, 'Kernel', 'Width', 4);
x = 0:1:25;
y = pdf(pd, x);
% Calculate the standard deviation of the data
std_dev = std(data);
% scale the standard deviation
scaled_std_dev = std_dev * max(y) / max(diameter_TAC);
% Plot the PDF
figure;
plot(x, y, 'k-', 'LineWidth', 2);
hold on;
% Add error bars
% plot them at each point of x.
errorbar(x, y, repmat(scaled_std_dev, size(x)), 'k.', 'LineWidth', 1);
Thank you so much.
What is this scale? can I simply divide all stds by 1000 to have a smaller bar on the graph?
The scale is to align the stds to the pdf value by scaling the stds value relative to the maximum value of the PDF. You can use the custom scaling factor to adjust the height.

Accedi per commentare.

Più risposte (0)

Richiesto:

il 20 Set 2024

Modificato:

il 20 Set 2024

Community Treasure Hunt

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

Start Hunting!

Translated by