MATLAB Answers

extracting information from a boxplot saved as fig

53 views (last 30 days)
CLARK KENDRICK GO
CLARK KENDRICK GO on 20 Jun 2019
Edited: Adam Danz on 5 Jan 2021
I have a boxplot that is saved as fig. and I do not have the original data anymore. How can I extract the data from this figure? Thanks

Answers (1)

Adam Danz
Adam Danz on 20 Jun 2019
Edited: Adam Danz on 5 Jan 2021
There's no way to access the raw data that were used to create the boxplots unless those values were intentionally saved somewhere in the figure (ie. UserData property) by whoever developed the code that produced those figures.
However, you can access the meta-data used to create the line objects on the plot such as the coordinates of the rectangle, the whisker coordinates, outlier coordinates, etc. You could use those values to simulate the raw data but that won't give you the original raw data.
Here's a demo that shows how to access the meta-data.
% First produce a figure and we'll pretend like you just opened it.
load carsmall %built-in matlab data
boxplot(MPG)
xlabel('All Vehicles')
ylabel('Miles per Gallon (MPG)')
title('Miles per Gallon for All Vehicles')
% get the handles to each line object
ax = gca(); %axis handle (assumes 1 axis)
bph = ax.Children; %box plot handle (assumes 1 set of boxplots)
bpchil = bph.Children; %handles to all boxplot elements
bpchil is a list of objects that create the boxplot
bpchil =
7×1 Line array:
Line (Outliers)
Line (Median)
Line (Box)
Line (Lower Adjacent Value)
Line (Upper Adjacent Value)
Line (Lower Whisker)
Line (Upper Whisker)
Note that the 3rd handle is to the "box". You can get the box coordinates by accessing the XData and YData. Do the same to get the coordinates of the other objects.
bh = findobj(gca,'type','line','tag','Box'); % Handle to the "box" lines
>> bh.XData
ans =
0.925 0.925 1.075 1.075 0.925
>> bh.YData
ans =
16.5 29 29 16.5 16.5
Alternatively, you can compute the boxplot statistics from the raw data including the median, upper and lower boxplot edges, and the whisker edges. This is demonstrated at the bottom of this answer (see Extracting location of boxs) using boxchart but you can do the same thing with boxplot by using the inputs.

Community Treasure Hunt

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

Start Hunting!