Sunday, October 4, 2009

Changing labels in a Matlab whisker box plot

Given my recent Matlab experiences, I am tempted turn this blog into a Matlab-hater blog, but I will try to keep my cool.

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:

Better, isn't it? Better, yes, but the way there couldn't be less intuitive.

11 comments:

  1. I agree... boxplots in Matlab are so infuriating!

    Do 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

    ReplyDelete
  2. 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.

    I have switched to gnuplot for my recent plotting needs.

    ReplyDelete
  3. 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!

    ReplyDelete
  4. FYI, I happened to stumble on the lable font size changing problem also this morning. The funny thing is that using:
    h = 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

    ReplyDelete
  5. Thank you! It gave me a weird error but did change the font size!

    ReplyDelete
  6. I also get this 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

    but it aint working.

    ReplyDelete
  7. 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)

    text_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!

    ReplyDelete
  8. solution (sort of)

    set(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])

    ReplyDelete
  9. try http://www.mathworks.com/matlabcentral/fileexchange/26400-function-readyforprint

    ReplyDelete
  10. To 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:

    set(gca,'xtick',[1 2 3],'xticklabel',{'Data A', 'Data B', 'Data C'});

    ReplyDelete