Plot of histogram and probability distribution function.
34 views (last 30 days)
Show older comments
Saurabh Sharma
on 11 Apr 2024
Commented: Saurabh Sharma
on 13 Apr 2024
I have three columns of data. I am plotting histogram and normal distribution probability density function (pdf) for each column. For one column, I get its histogram and pdf shown by two different colours in plot and I get two indicators in legend. Similarly, I get histogram and pdf for remaining two columns of data. In total, I get 6 indicators with 6 different colors in legend. I want to get histogram and pdf of one column of data with same colour and show indicator only for histogram. Similarly, I want to get histogram and pdf for remaining two columns so that I get three colours for three columns of data.
Thankyou
x = (1:1:100)';
y = (101:1:200)';
z = (201:1:300)';
p1 = fitdist(x,'Normal');
p2 = fitdist(y,'Normal');
p3 = fitdist(z,'Normal');
plot(p1)
hold on
plot(p2)
plot(p3)
legend('5%','5%','10%','10%','15%','15%');
hold off
0 Comments
Accepted Answer
Adam Danz
on 12 Apr 2024
Edited: Adam Danz
on 12 Apr 2024
To equate the colors between the histogram and line pairs, access the object handles from the plot output and then set the SeriesIndex property to the same value. See a note at the bottom of this solution to control the color.
To show only the histogram objects in the legend, specify the histogram handles in the legend command. I also defined the legend strings using the DisplayName properties. Note that the fitdist plot will return a 1x2 vector of handles where the first handle is the line object and the second is the histogram.
x = (1:1:100)';
y = (101:1:200)';
z = (201:1:300)';
p1 = fitdist(x,'Normal');
p2 = fitdist(y,'Normal');
p3 = fitdist(z,'Normal');
h1 = plot(p1);
set(h1,'SeriesIndex',1)
h1(2).DisplayName = '5%';
hold on
h2 = plot(p2);
set(h2,'SeriesIndex',2)
h2(2).DisplayName = '10%';
h3 = plot(p3);
set(h3,'SeriesIndex',3)
h3(2).DisplayName = '15%';
legend([h1(2),h2(2),h3(2)]);
hold off
ax = gca();
ax.ColorOrder = [1 0 0; 0 0 1; 1 0 1];
More Answers (0)
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!