using fill function to make shaded area

21 views (last 30 days)
Hi every one!
Is it possible to draw a shaded area (e.g., +- STD of a value) when we have a categorical values on x-axis?
Based on the screenshot I want AA,BB,CC instead of the numbers.(dash line is a value and the shaded area around it can be plus/minus STD)
Many thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Nov 2022
x = categorical({'AA', 'BB', 'CC'});
yraw = rand(50,3);
ymean = mean(yraw,1);
plot(x, ymean, 'b');
s = std(yraw, [], 1);
hold on
fill([x, fliplr(x)], [ymean-s, fliplr(ymean+s)], 'k', 'FaceAlpha', 0.1 )
hold off
  4 Comments
Walter Roberson
Walter Roberson on 2 Nov 2022
Use reordercats to get the order the way you want. The default order is alphanumeric, and dp1200 is before dp500 because the comparison proceeds from the beginning and '1' is before '5'
would it be possible to start a bit before AA and ends a bit after CC
You would need to add additional categories -- for example if you wanted to start 1/10 before AA you would need 32 or so categories in which AA was the 2nd category, CC was the 31'st category, and BB was the middle. And you would probably need to use xticks([AA, BB, CC]) so you got ticks at the right place.
The alternative would be to switch to a numeric axes instead of a categorical axes, and use xticks() to place the marks numerically and xlabels() to write in the appropriate category names.
Ham Man
Ham Man on 2 Nov 2022
Thank you so much Walter. That works.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!