Overlay a scatterplot of continuous data with boxplots summarizing groupings of the same data

I have a scatterplot of continous x-y data values, where the x data is time
x=[1:100];
and the y data are measured data values at each time point. For example Y data, lets use 100 random values between 150 and 200:
y = (200-150).*rand(100,1) + 150)
Separately, I've also split the y-value data into 3 groups based on which segment of time they were meausured (group1: x-values from 0-33, group2: x-values from 33-66, etc), and calculated summary statistics of the corresponding y-data for each group. I would like to overlay a boxplot of the summary statistics for each group onto the scatterplot of the continuous data.
figure; ax1=scatter(x, y, [ ], [0.5 0.5 0.5], 'filled', 'MarkerFaceAlpha', .05, 'MarkerFaceColor',[0.5 0.5 0.5] );
xticks(0:20:100);
This scatterplot has xticks and xticklabels from [0:20:100].
I then use "hold on" to create a boxplot over the scatterplot, where I specify the placement of the boxes along the x-axis of the scatterplot to correspond to the center x-value of each group.
x_group_labels=vertcat(ones(1,33)',ones(1,33)'*2,ones(1,34)'*3);
hold on; boxA=boxplot(y, x_group_labels, 'symbol', '','Positions', [17,49.5,83]);
However, while the boxplots are properly placed over the scatterplot data in the correct figure location (x=[17,49.5,83] of the scatterplot), the xticks and xlabels have changed so that only the boxplot group number (e.g. "1", "2" ,"3") is visible under each boxplot.
I would like to keep the original x-axis display of the scatterplot and not the boxplot but try as I might, I'm not able to figure out how to accomplish this. Any assistance or tricks would be immensely appreciated.

 Accepted Answer

Fortunately, I just figured out something that works, so I guess I can answer my own question. Adding this line after the plotting the boxplot on top of the scatter plot reverts the x-axis labels back to what was shown on the underlying scatterplot:
set(gca,'XTickLabelMode','auto','XTickMode','auto');
I can't say why or how it works, but it does, so I'll take it!

1 Comment

Congratulations on finding your solution. Sometimes Matlab can be mysterious, so we go with what works! : - )

Sign in to comment.

More Answers (1)

Have you tried plotting the boxplot on the secondary y-axis and hide its x-labels?

1 Comment

Thanks for the suggestion CAM. I've tried a few ways. If I do either:
yyaxis right;
boxA=boxplot(y, x_group_labels, 'symbol', '','Positions', [17,49.5,83]);
xticklabels([]);
or:
yyaxis right;
boxA=boxplot(y, x_group_labels, 'symbol', '','Positions', [17,49.5,83]);
ax2=get(gca);
ax2.XAxis.Visible='off';
both options still remove the figure's x-axis ticks/labels, not just the boxplot's ticks/labels.
And in all cases I've tried, it doesn't work to simply re-set the x-axis ticks as:
set(gca,'xtick',[0:20:100]);

Sign in to comment.

Products

Release

R2021b

Asked:

on 16 Feb 2023

Commented:

CAM
on 16 Feb 2023

Community Treasure Hunt

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

Start Hunting!