Histogram: put mean and std in the legend

7 次查看(过去 30 天)
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)

回答(1 个)

Gareth
Gareth 2018-11-20
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);

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by