MATLAB Answers

display the data points to a boxplot

125 views (last 30 days)
geethu th
geethu th on 9 Jan 2019
Edited: jonas on 11 Jan 2019
I have the following data points:
x=[1,1,1,1,1,1,1,1,1,1,1,1,24,24,24,24,24,24,24,24,24,24,24,24,24,72,72,72,72,72,72,72,72,72,72,72,72,72,120,120,120,120,120,120,120,120,120,120,120,120,120,168,168,168,168,168,168,168,168,168,168,168,168,168,360,360,360,360,360,360];
y1=[3.4,3.6,10.4,8.6,15.2,20,4.6,8.8,8.4,1.6,0.6,3.6,1.07,0.28,0.75,3.48,0.43,3.17,3.58,0.77,0.59,2.19,0.17,0.25,0.56,2,0.18,0.56,3.93,2.28,2.23,2.39,2.51,0.31,1.04,0.08,0.2,0.56,1.6,0.28,0.39,3.34,1.86,2.22,2.72,2.21,0.25,0.63,0.48,0.14,0.53,1.45,0.22,0.53,2.54,1.45,1.8,2.29,2.2,0.18,0.49,0.48,0.17,0.42,1.04,0.13,0.63,2.06,0.25,0.33];
C=[14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29];
I would like to plot loglog plot of x and C. and boxplot of x and y1. I need both in the same figure. Also I want to display the data ponts o boxplot to be display on boxplot to understant the distribution. My code is:
x=[1,1,1,1,1,1,1,1,1,1,1,1,24,24,24,24,24,24,24,24,24,24,24,24,24,72,72,72,72,72,72,72,72,72,72,72,72,72,120,120,120,120,120,120,120,120,120,120,120,120,120,168,168,168,168,168,168,168,168,168,168,168,168,168,360,360,360,360,360,360];
y1=[3.4,3.6,10.4,8.6,15.2,20,4.6,8.8,8.4,1.6,0.6,3.6,1.07,0.28,0.75,3.48,0.43,3.17,3.58,0.77,0.59,2.19,0.17,0.25,0.56,2,0.18,0.56,3.93,2.28,2.23,2.39,2.51,0.31,1.04,0.08,0.2,0.56,1.6,0.28,0.39,3.34,1.86,2.22,2.72,2.21,0.25,0.63,0.48,0.14,0.53,1.45,0.22,0.53,2.54,1.45,1.8,2.29,2.2,0.18,0.49,0.48,0.17,0.42,1.04,0.13,0.63,2.06,0.25,0.33];
C=[14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,14.82,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,4.29,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.79,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2.29,2,2,2,2,2,2,2,2,2,2,2,2,2,1.49,1.49,1.49,1.49,1.49,1.49];
loglog(x,C,'LineWidth',3,'Color','c')
hold on
plot(x,y1,'o')
hold on
boxplot(y1, x)
hold on
I got the following output.
I dont know why only the first boxplot points are showing. I have made a rough sketch of what the required figure is as follows.
Can anybody help me? I am stuck with this for the last two days.

  0 Comments

Sign in to comment.

Accepted Answer

jonas
jonas on 9 Jan 2019
Edited: jonas on 9 Jan 2019
The boxplot is not plotted against the correct x-values. The inserted x-array is treated as the grouping variable and the ticks are labelled accordingly, however the boxes are still plotted on x = [1 2 3 4...]. You can use the 'position' property to set the x-values.
figure;hold on
plot(x,y1,'o')
plot(x,C,'LineWidth',3,'Color','c')
boxplot(y1,x, 'positions',x)
set(gca,'xscale','log')
unfortunately the width of the boxes become inconsistent when setting the xscale to log.

  6 Comments

Show 3 older comments
geethu th
geethu th on 10 Jan 2019
Jonas,
I got the following outpput.
Capture.jpg
Is it possible to display the median, quantile values(Q1 and Q3) values on each of these boxplot?
jonas
jonas on 11 Jan 2019
Possible, yep. I'd probably do the following
box = findobj(gca,'tag','Box')
for i=1:numel(box)
prctls(i,1:2) = unique(box(i).YData)
end
prct25 = prctls(:,1);
prct75 = prctls(:,2);
Then you'd have all the ydata, and can plot the values using the text(x,y,'string') function.
text(flip(unique(log10(x))),prct25,flip(sprintfc('%.2g',prct25)))
You can then do the same for the median, just adapt the code ('tag' should be 'Median')

Sign in to comment.

More Answers (0)

Sign in to answer this question.