How to add an additional tick on y-axis for a combined boxplot and scatter plot?

6 views (last 30 days)
Hello all,
I've got 15 horizontal boxplots that reach up until y=1.6 and a scatter plot on the same figure, where the scatter plot are some x values at y=2 only.
I can see the boxplot ticks on the y-axis up until y=1.6. However, I cannot see a ytick on the y-axis at y=2. How can I add that?
**************************
My code until now is:
Ypos = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.5 1.6];
group_fordata = [repmat(Ypos(1),[size(x1),1]);
repmat(Ypos(2),[size(x2)],1); repmat(Ypos(3),[size(x3)],1);
repmat(Ypos(4),[size(x4)],1); repmat(Ypos(5),[size(x5)],1);
repmat(Ypos(6),[size(x6)],1); repmat(Ypos(7),[size(x7)],1);
repmat(Ypos(8),[size(x8)],1); repmat(Ypos(9),[size(x9)],1);
repmat(Ypos(10),[size(x10)],1); repmat(Ypos(11),[size(x11)],1);
repmat(Ypos(12),[size(x12)],1); repmat(Ypos(13),[size(x13)],1);
repmat(Ypos(14),[size(x14)],1); repmat(Ypos(15),[size(x15)],1)];
data = [x1;x2;x3;x4;x5;x6;x7;x8;x9;x1;x11;x12;x13;x14;x15]
figure
boxplot(data, group_fordata,'positions', Ypos,'orientation','horizontal')
hold on
scatter([0.4 0.6],[2.0 2.0],'g.')
ylim([0 2.1])

Accepted Answer

Chad Greene
Chad Greene on 2 Jun 2015
This solution's a little clunky, but it works:
% Create a label string for each Ypos:
for k = 1:numel(Ypos)
NewTickLabels{k} = sprintf('%0.1f',Ypos(k));
end
% Include 2.0:
NewTickLabels{k+1} = sprintf('%0.1f',2);
% Set tick values and labels:
set(gca,'YTick',[Ypos 2],'YTickLabel',NewTickLabels)

More Answers (1)

Chad Greene
Chad Greene on 29 May 2015
Edited: Chad Greene on 29 May 2015
You can specify Ytick locations explicitly like this:
Ypos = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.5 1.6];
plot(rand(10,2),'bo')
ylim([0 2.1])
set(gca,'Ytick',[Ypos 1.7334 2])
  3 Comments
Christina
Christina on 2 Jun 2015
Hi Chad,
Below there is an example with dummy data. Still in the position of my scatter two points, instead of 2.0 as a tick on the y-axis, I've got 0.0.
*********************************************************************
lower = 0;
upper = 1.6;
%%%%%%%%%%%%%%%%%%%%%
%% Actual boxplot data
x1 = lower + (upper - lower).*rand(200,1);
x2 = lower + (upper - lower).*rand(190,1);
x3 = lower + (upper - lower).*rand(180,1);
x4 = lower + (upper - lower).*rand(170,1);
x5 = lower + (upper - lower).*rand(160,1);
x6 = lower + (upper - lower).*rand(150,1);
x7 = lower + (upper - lower).*rand(140,1);
x8 = lower + (upper - lower).*rand(130,1);
x9 = lower + (upper - lower).*rand(120,1);
x10 = lower + (upper - lower).*rand(110,1);
x11 = lower + (upper - lower).*rand(100,1);
x12 = lower + (upper - lower).*rand(90,1);
x13 = lower + (upper - lower).*rand(80,1);
x14 = lower + (upper - lower).*rand(70,1);
x15 = lower + (upper - lower).*rand(60,1);
data = [x1;x2;x3;x4;x5;x6;x7;x8;x9;x10;x11;x12;x13;x14;x15];
%%%%%%%%%%%%%%%%%%
%% Positions on the y-axis
Ypos = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.5 1.6];
%%%%%%%%%%%%%%%%
% Data groups
group_fordata = [repmat(Ypos(1),[size(x1),1]);... repmat(Ypos(2),[size(x2)],1); repmat(Ypos(3),[size(x3)],1);... repmat(Ypos(4),[size(x4)],1); repmat(Ypos(5),[size(x5)],1);... repmat(Ypos(6),[size(x6)],1); repmat(Ypos(7),[size(x7)],1);... repmat(Ypos(8),[size(x8)],1); repmat(Ypos(9),[size(x9)],1);... repmat(Ypos(10),[size(x10)],1); repmat(Ypos(11),[size(x11)],1);... repmat(Ypos(12),[size(x12)],1); repmat(Ypos(13),[size(x13)],1);... repmat(Ypos(14),[size(x14)],1); repmat(Ypos(15),[size(x15)],1)];
%%%%%%%%%%%%%%%%
%%Plotting the boxplots and the scatter points
figure
boxplot(data, group_fordata,'positions',Ypos,'orientation','horizontal')
hold on
scatter([0.4 0.6],[2.0 2.0],'g.')
ylim([0 2.1])
set(gca,'Ytick',[Ypos 2.0])

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!