xlabel in box plot
Show older comments
Hello all, I need help concerning the xlabel in my box plot. I am using this (part of) code:
boxplot(AB, 'Labels',{'1','2','3','4','5','6','7','8','9','10','11','12'})
xlabel('X Y Z')
So, if possible, I need X under 1 2 3 4, Y under 5 6 7 8 and Z under 9 10 11 12 with X, Y and Z entered. Could anyone please help me with this subject?
Greetings, Paulo Sousa
Answers (1)
dpb
on 12 Jul 2015
Like
text([2.5:4:12].',-0.1*ones(3,1),['X';'Y';'Z'],'horizontal','center')
maybe?
4 Comments
Paulo Sousa
on 13 Jul 2015
Did you try it!!!???
I don't know what your AB is so no idea on what the y-scale is so for testing I just used
AB=rand(12);
before the boxplot call as you wrote it. That meant the ylim range on the y-axis was 0-1 which is why the text y-position of -0.1 worked well here. Obviously, move that based on your y-axis scale; apparently -0.4 worked; it's also possible to make those positions absolute instead of scaled to the axes or to compute a location based on the scale factor to make them invariant to data if you're doing this more than once. If you do desire that someone really try to duplicate your plot, attach the data as a .mat file so can actually create the same plot as you have.
I don't follow the 'A','B','C' above when you then say you want 'X' so not at all clear what you actually want the end result to be. If you're trying to show groupings, I'd probably change the labels to be
boxplot(AB, 'Labels',{'{1','2','3','4}','{5','6','7','8}','{9','10','11','12}'})
to put the curlies around the labels to indicate the groups there essentially automagically. The x- position as [2.5:4:12] combined with 'horizontal','center' serves to ensure the text written is centered at the location on the x axis that is precisely on the midpoint; you don't need to try to "fudge" on the position to make the characters appear where you want based on font and fontsize, etc., etc., etc., ...; just use the alignment property to center whatever is written at the correct position.
If you want to draw a line as well, then line will let you do that as well or you could alternatively to individual text/line pieces use one of the arrows available with the annotation object. See
doc annotation % for details
Matlaber
on 30 Apr 2020
can you make the text lable in italic?
dpb
on 30 Apr 2020
Look at the properties of the object -- 'FontStyle'
Categories
Find more on Annotations 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!