Cannot overlay line of best fit over boxplot

4 views (last 30 days)
In the script below;
% Fit line of best fit to median data
logData=log(Data+1); y=median(logData); x=j; [P,S]=polyfit(x,y,1); [Y,DELTA]=polyval(P,x,S);
% Plot everything
ii=ii+1; figure(ii) orient Landscape; set(gcf,'units', 'normalized', 'outerposition', [0 0 1 1]);
%boxplot(logData, num2cell(int16(j))) boxplot(logData, int16(j)), hold on
plot(x,Y, 'c','LineWidth', 3) plot(x,Y+DELTA,'r-') plot(x,Y-DELTA,'r-') title('log(BEFORE Data + 1)') hold off boxplot plots correctly and the line of best fit, if plotted on a separate figure also plots correctly. However the line of best fit is NOT overlaid on the boxplot as intended. I think that I am using hold on and hold off correctly? Why is this not working?

Accepted Answer

Star Strider
Star Strider on 16 Feb 2015
I can’t run your code (and I don’t have the energy just now to try to re-create your data), so it may be that all the ‘hold on’ commands could be the problem. Just one call to each should suffice.
Try this instead:
figure(ii)
orient Landscape;
set(gcf,'units', 'normalized', 'outerposition', [0 0 1 1]);
boxplot(logData, int16(j))
hold on
plot(x,Y, 'c','LineWidth', 3)
plot(x,Y+DELTA,'r-')
plot(x,Y-DELTA,'r-')
title('log(BEFORE Data + 1)')
hold off
  4 Comments
BGC
BGC on 17 Feb 2015
Hi Star Strider, I have found the solution! I modified the boxplot command to "link" the positions of the boxes with the x axis of the line of best fit. The modified boxplot command is now; boxplot(logData, int16(j),'positions',x) ..and that works! Thanks for your help. BGC

Sign in to comment.

More Answers (1)

Helen Kourous
Helen Kourous on 30 Mar 2015
"Hold on" does not seem to work well with boxplots, even when you presize the axes. (Even tried "hold all")

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!