Histogram: put mean and std in the legend

9 views (last 30 days)
alpedhuez
alpedhuez on 4 Apr 2018
Answered: Gareth on 20 Nov 2018
In the code below I will have histograms of three series. I want to have mean and std of three series calculated and displayed in the legend of each series. Please advise.
w=0:0.05:1;
ncount1 = histc(x(:,1),w);
relativefreq1=ncount1/NSamples;
ncount2 = histc(x(:,2),w);
relativefreq2=ncount2/NSamples;
ncount3 = histc(x(:,3),w);
relativefreq3=ncount3/NSamples;
figure (4)
plot(w, relativefreq1)
hold on
plot(w, relativefreq2,'LineWidth',1.0)
hold on
plot(w, relativefreq3,'LineWidth',1.5)
legend('Data1','Data2','Data')
title('Distribution of Data')
xlabel('Data')
ylabel('Frequency')
xlim([min(w) max(w)])
set(gca, 'xtick', w)

Answers (1)

Gareth
Gareth on 20 Nov 2018
I am using R2018b and my understanding is that you want to have:
  • each line on your plot not named data, but the legend itself has the value of the mean/std of the line
If this is the case maybe this example might help you:
data = rand(10,3); %as the data here is not relevant to your question
plot(data);
mymean = mean(data);
mystd = std(data);
% Create Legend
mylegend = "std: " + string(mystd) + " mean: " + string(mymean);
%add to plot
legend(mylegend);

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!