Tuesday, April 7, 2009

Matlab boxplot: how to change the line width (LineWidth) and color of the lines?

It was hard for me to find the solution of this problem using Matlab help. Finally I found the solution and writing here so that it can be useful for you.

The following example creates four plots. Simple copy and paste the whole code and see what it does.

---
clear all
close all
a=rand(5)*100
%figure To create default box plot of matlab
subplot 221
h=boxplot(a)
%figure To change the linewidth of the box plot,
subplot 222
hl=boxplot(a)
for ih=1:6
set(hl(ih,:),'LineWidth',2);
end
%figure To change the color of the blox plot, here green is used
subplot 223
hc=boxplot(a,'Color','g')
%figure, To change colour as well as linewidth
subplot 224
hlc=boxplot(a,'Color','r')
for ih=1:6
set(hlc(ih,:),'LineWidth',2);
end


This example creates a plot which looks like; you can choose the option which fits you.


12 comments:

  1. Thanks, that's a smart way. You can avoid the loop by writing
    set(hl(1:6,:),'LineWidth',2);

    ReplyDelete
  2. Thanks man! rightaway what i was looking for! keep posting!

    ReplyDelete
  3. Thanks for the post! I only don't understand why the function is boxplot(X,G,'color',...) and not 'colors'? There is nothing in the manual about 'color'. Has it changed, because I'm using 2010a?

    Maybe offtopic, but I found that if you put a nx3 matrix Mrgb e.g.
    [0.5 0.5 0.5;
    1 1 1;
    0 1 0] etc. when you use boxplot(X,G,'colors',Mrgb) you essentially get a third dimension in your boxplot. So, it's like a pseudo 3D boxplot.
    It turned out really useful for me when I had X values of different lengths (see here for solution: http://www.mathworks.com/support/solutions/en/data/1-7SP1DM/index.html?product=ML)

    ReplyDelete
  4. Thank you! That is exactly what I am looking for :)

    ReplyDelete
  5. MATLAB helps are suck.


    I agree. Thank a lot for your post

    ReplyDelete
    Replies
    1. May be it was too complicated for describing the handls of the function and the relationship about gca & gcf.

      Delete
  6. Thanks! Very helpful and impossible to find elsewhere. Why is ih cycled through 1:6 in the for loop?

    ReplyDelete
  7. article that you created is very interesting, by reading this article I have a reference to write a new post on my blog

    ReplyDelete
  8. this routine loses the 'whisker' value, and resets it to the default 1.5

    ReplyDelete
  9. That's exactly what I was looking for. However, I need to do the same in Octave.
    Any thoughts? Thnx.

    ReplyDelete