Here's the next trick that I hope will save someone else a lot of time. It's about changing the label in a whisker box plot.
If you have the statistics toolbox installed, you can create whisker box plots with the boxplot() command.
Let's try:
>> boxplot(rand(10, 3), 'labels', {'Data A', 'Data B', 'Data XYZ'})
This creates a whisker box plots with the individual boxes labeled 'Data A', 'Data B' and 'Data XYZ':

Looks great, until you realize that you meant to label the third box 'Data C', and not 'Data XYZ'. Should be straight-forward to change, right? Isn't. Five minutes of clicking around and swearing took me nowhere.
So here's how to change the label. In the figure, click on Edit -> Figure properties. Next, click on any of the boxes. All boxes and all labels will be selected. Now, in Matlab's main window enter:
>> children = get(gco, 'Children');
This gives you a handle to all children of the currently selected object. The object you have selected right now is the boxplot, which is of type hggroup. Its i-th child is the label of the (n-i)-th box. So, the label of the the third box in the figure created above is the first child. It can be changed with
>> set(children(1), 'String', 'Data C');
And finally:

I agree... boxplots in Matlab are so infuriating!
ReplyDeleteDo you happen to know how to change the fontsize of the labels? You would think it would follow a similar routine of what you did here, but surprise.... not that simple.
Any help you can provide would be great!
~Mark
Mark, I am sorry, but I don't know about changing the font size. I guess that font size is also a property of the children(1) object, but I don't know for sure.
ReplyDeleteI have switched to gnuplot for my recent plotting needs.
has anyone figured out how to change the fontsize of the labels since these posts? I don't see why when I change the fontsize of the figure it only changes the fontsize on the y-axis. Really frustrating!
ReplyDeleteFYI, I happened to stumble on the lable font size changing problem also this morning. The funny thing is that using:
ReplyDeleteh = boxplot(..., 'labels', MyLabels);
gives a handle to everything in the boxplot except for the lables... Using the info from this post and taking some time to understand the axes-object-children hierarchy, here is what I got.
figure
boxplot(...)
h = findobj(gca);% to get a handle to the objects
children = get(h(2),'children');%In my case boxplot happens to be the second object of the plot.
for i = 1:length(MyLabels)
set(children(i), 'FontSize', 12, 'FontName','Whatever');%To change label properties
end
HTH,
Jonathan
Thank you! It gave me a weird error but did change the font size!
ReplyDeleteI also get this h = findobj(gca);% to get a handle to the objects
ReplyDeletechildren = get(h(2),children');%In my case boxplot happens to be the second object of the plot.
for i = 1:length(MyLabels)
set(children(i), 'FontSize, 12, 'FontName','Whatever');%To change label properties
end
but it aint working.
If you have your boxplot as the current figure then the below will work to change the font size of the x-labels (took me ages to work this out though)
ReplyDeletetext_h=findobj(gca,'type','text')
set(text_h,'FontSize',16)
BUT I now have a HUGE problem in that my larger fontsize labels overlap the x-axis and I can't figure out how to reposition them, even worse when I scale the window the labels don't automatically reposition themselves in the way Matlab promises me they will (the failure of automatic repositioning problem was happening before I changed the font size). Help!
solution (sort of)
ReplyDeleteset(text_h(1),'Position',get(text_h(1),'Position') - [0 5 0])
>> set(text_h(2),'Position',get(text_h(2),'Position') - [0 5 0])
>> set(text_h(3),'Position',get(text_h(3),'Position') - [0 5 0])
try http://www.mathworks.com/matlabcentral/fileexchange/26400-function-readyforprint
ReplyDeleteTo the OP, there's quite an easy way to change the tick labels. I'd recommend not claiming MATLAB doesn't make it easy just because you don't know the way. It takes just one line of code:
ReplyDeleteset(gca,'xtick',[1 2 3],'xticklabel',{'Data A', 'Data B', 'Data C'});
christian louboutin outlet
ReplyDeletefitflop shoes
nike outlets
abercrombie
jordan shoes for sale
boys north face jackets
columbia outlet
louboutin outlet
coach shoes
nike air max
joes new balance outlet
lebron 12
celine bags
polo ralph lauren factory store
nike air max
mont blanc mountain
coach purses
cheap nike shoes
jordan shoes for women
coach factory outlet online
fitflop sandals for women
fitflops clearance
michael kors cheap
fitflops sale
lebron 12 shoes
celine handbags
fitflops clearance
gucci belt
nike outlet store online shopping
kobes shoes
toms shoes
christian louboutin shoes
coach handbags outlet
hollister clothing store online
jordan retro 6
salvatore ferragamo belt
louboutin outlet
michael kors outlet
fendi bags
coach factory outlet
cheap polo shirts
the north face jackets
tod's
ugg boots
20151012yxj