How to clear boxplots?

10 views (last 30 days)
Petra
Petra on 7 Dec 2012
Dear Matlab-Users
Is there a way to efficiently delete all elements of a boxplot from a figure? This implementation does the job
delete(findobj(gca,'Tag','Box')); delete(findobj(gca,'Tag','Upper Adjacent Value')); delete(findobj(gca,'Tag','Lower Adjacent Value')); delete(findobj(gca,'Tag','Upper Whisker')); delete(findobj(gca,'Tag','Lower Whisker')); delete(findobj(gca,'Tag','Median')); delete(findobj(gca,'Tag','Outliers')); delete(findobj(gca,'Type','patch')); % the boxes are colored
But it is very slow and not feasible, since I deal with many axes (which are integrated in a GUI). This GUI allows the user to adjust the matrix X which is depicted by boxplot(X). I think that cla is not an option, since the axes contain other elements which need to stay visible. Any ideas?
boxplot(X,'Tag','box_plot'); delete(findobj(gca,'Tag','box_plot'));
is not allowed. How can I avoid this frequent call of findobj?
Thanks in advance.
Petra

Answers (1)

Jonathan Epperl
Jonathan Epperl on 7 Dec 2012
You should capture the handles of the objects created by boxplot in a variable, then you can delete them all together without having to find them every time:
load carsmall
h = boxplot(MPG,Origin); % Now h contains the handles to every object created
text(3, 40,'blabla')
delete(h) % Note that the boxes and stuff are gone, the text is still there
  1 Comment
Matt Fig
Matt Fig on 7 Dec 2012
Petra, with graphics objects (figures, axes, lines, etc) it is always worthwhile to see if the creation call returns a handle. This will help you in further manipulations down the road, more than just deleting things....

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!