From: Dustin on
> I think it's doing what you want it to do, but you need to give it x positions and widths to position the boxes in the correct area. The below should do that, as well as correct the annoying hijacking of the x-ticks (I hate that boxplot does that). Also, unless you plan to use varying colors and sizes in your scattered data, plot is a lot faster than scatter.
>
> x = rand(1000,1) .* 25;
> y = sin(x/4) + randn(size(x));
>
> edge = 0:.5:25;
> [n,bin] = histc(x, edge);
>
> xmid = (edge(1:end-1) + edge(2:end))./2;
> dx = diff(edge);
>
> figure;
> plot(x,y,'k.');
> hold on;
> boxplot(y, bin, 'positions', xmid, 'widths', dx);
> set(gca, 'xticklabelmode', 'auto', 'xtick', 0:5:25);

Thank you, this is exactly what i wanted but it is still not working for me. I am now getting a new error message. I'm starting to go crazy, i've even gone as far as mapping my data to variables x and y so i could use your code verbatim. The code i get is:

??? Error using ==> boxplot>assignPositions at 1592
'positions' parameter must be empty, the same length as the number of groups, or the same length as the number of points

By groups it means number of bins correct? Is this saying length(xmid) should be equal to length(n)? I come up with 50 and 51 respectively (the same as in your example).

The only thing that I can think of, is that, some of bins will be empty. for example i may or may not have any data between x=5 and 6, or no data above x = 17. Is this what is giving me problems?


I also tried the other method using grpstats and i receive the same error.
From: Kelly Kearney on
"Dustin " <dbrisset(a)gmail.com> wrote in message <i41hfa$l6d$1(a)fred.mathworks.com>...
> > I think it's doing what you want it to do, but you need to give it x positions and widths to position the boxes in the correct area. The below should do that, as well as correct the annoying hijacking of the x-ticks (I hate that boxplot does that). Also, unless you plan to use varying colors and sizes in your scattered data, plot is a lot faster than scatter.
> >
> > x = rand(1000,1) .* 25;
> > y = sin(x/4) + randn(size(x));
> >
> > edge = 0:.5:25;
> > [n,bin] = histc(x, edge);
> >
> > xmid = (edge(1:end-1) + edge(2:end))./2;
> > dx = diff(edge);
> >
> > figure;
> > plot(x,y,'k.');
> > hold on;
> > boxplot(y, bin, 'positions', xmid, 'widths', dx);
> > set(gca, 'xticklabelmode', 'auto', 'xtick', 0:5:25);
>
> Thank you, this is exactly what i wanted but it is still not working for me. I am now getting a new error message. I'm starting to go crazy, i've even gone as far as mapping my data to variables x and y so i could use your code verbatim. The code i get is:
>
> ??? Error using ==> boxplot>assignPositions at 1592
> 'positions' parameter must be empty, the same length as the number of groups, or the same length as the number of points
>
> By groups it means number of bins correct? Is this saying length(xmid) should be equal to length(n)? I come up with 50 and 51 respectively (the same as in your example).
>
> The only thing that I can think of, is that, some of bins will be empty. for example i may or may not have any data between x=5 and 6, or no data above x = 17. Is this what is giving me problems?
>
>
> I also tried the other method using grpstats and i receive the same error.

Yes, the error is arising due to an empty bin or two. One more try, this time with some data gaps:

x = rand(1000,1) .* 25;
x(x >= 5 & x <= 6) = [];
y = sin(x/4) + randn(size(x));

edge = 0:.5:25;
[n,bin] = histc(x, edge);

xmid = (edge(1:end-1) + edge(2:end))./2;
dx = diff(edge);

% Histc returns an "extra" bin that includes data that falls exactly on the
% last edge. I'm assuming for visualization purposes that this data
% belongs in the length(edge)-1 bin.

bin(bin == length(n)) = length(n)-1;

% Now eliminate the positions of empty bins

isemp = n(1:end-1) == 0;

xmid = xmid(~isemp);
dx = dx(~isemp);

figure;
plot(x,y,'k.');
hold on;
boxplot(y, bin, 'positions', xmid, 'widths', dx);
set(gca, 'xticklabelmode', 'auto', 'xtick', 0:5:25);
From: Dustin on
"Kelly Kearney" <kakearney(a)nospamgmail.com> wrote in message
> Yes, the error is arising due to an empty bin or two. One more try, this time with some data gaps:
>
> x = rand(1000,1) .* 25;
> x(x >= 5 & x <= 6) = [];
> y = sin(x/4) + randn(size(x));
>
> edge = 0:.5:25;
> [n,bin] = histc(x, edge);
>
> xmid = (edge(1:end-1) + edge(2:end))./2;
> dx = diff(edge);
>
> % Histc returns an "extra" bin that includes data that falls exactly on the
> % last edge. I'm assuming for visualization purposes that this data
> % belongs in the length(edge)-1 bin.
>
> bin(bin == length(n)) = length(n)-1;
>
> % Now eliminate the positions of empty bins
>
> isemp = n(1:end-1) == 0;

These last two lines are the key!

> xmid = xmid(~isemp);
> dx = dx(~isemp);
>
> figure;
> plot(x,y,'k.');
> hold on;
> boxplot(y, bin, 'positions', xmid, 'widths', dx);
> set(gca, 'xticklabelmode', 'auto', 'xtick', 0:5:25);

Thank you so much for your help and your patience with me. You should get a gold star.