Box plot labels not working for hold on

19 views (last 30 days)
Hello,
I'm trying to create a box plot from an array in a text file. However I'm having an issue with my x tick labels. They're only being shown for the third set of data. Any ideas?
(P.s. I'm fairly new to Matlab so sorry if my code is long winded, I know it could have been written in a much nicer way!!)
Thanks in advance
Tab = readtable("Validation.txt");
TABLE=table2array(Tab);
Data=TABLE(:,:);
X1= [1 2 3];
X2=[4 5 6];
X3=[7 8 9];
hold all
boxplot(Data(:,1:3),'notch', 'on','Colors',"r","Positions",X1,'Labels',{'1mm A','1mm B','1mm C'})
boxplot(Data(:,4:6),'notch','on','Colors',"b",'Positions',X2,'Labels',{'1.5mm A','1.5mm B','1.5mm C'})
boxplot(Data(:,7:9),'notch','on','Colors',"g",'Positions',X3,'Labels',{'3mm A','3mm B','3mm C'})
xlim([0 10]);
set(gca,'FontSize',10,'XTickLabelRotation',90)

Accepted Answer

Hannah Taylor
Hannah Taylor on 24 Feb 2021
Managed to fix this (for anyone that stumbles across a similar issue in the future!). The issue was that the figure was only displaying three ticks (the final 3) and so when it went to assign tick labels, it only assigned them to the final three. I inserted the final line:
boxplot(Data(:,1:3),'notch', 'on','Colors',"r","Positions",X1)
boxplot(Data(:,4:6),'notch','on','Colors',"b",'Positions',X2)
boxplot(Data(:,7:9),'notch','on','Colors',"g",'Positions',X3)
xticklabels({'1mm A','1mm B','1mm C','1.5mm A','1.5mm B','1.5mm C','3mm A','3mm B','3mm C'})
set(gca,'xtick',1:9); %inserted this line!!
Which gave the plot 9 ticks, allowing all 9 tick labels to be displayed.

More Answers (1)

randerss simil
randerss simil on 23 Feb 2021
Edited: randerss simil on 23 Feb 2021
Tab = readtable("Validation.txt");
TABLE=table2array(Tab);
Data=TABLE(:,:);
X1= [1 2 3];
X2=[4 5 6];
X3=[7 8 9];
hold all
boxplot(Data(:,1:3),'notch', 'on','Colors',"r","Positions",X1)
boxplot(Data(:,4:6),'notch','on','Colors',"b",'Positions',X2)
boxplot(Data(:,7:9),'notch','on','Colors',"g",'Positions',X3)
xticklabels({'1mm A','1mm B','1mm C','1.5mm A','1.5mm B','1.5mm C','3mm A','3mm B','3mm C'})
xlim([0 10]);
set(gca,'FontSize',10,'XTickLabelRotation',90)
Try the above
  3 Comments
randerss simil
randerss simil on 24 Feb 2021
Edited: randerss simil on 24 Feb 2021
Tab = readtable("Validation.txt");
TABLE=table2array(Tab);
Data=TABLE(:,:);
X1= [1 2 3];
X2=[4 5 6];
X3=[7 8 9];
hold all
boxplot(Data(:,1:3),'notch', 'on','Colors',"r","Positions",X1);xticklabels({'1mm A','1mm B','1mm C'})
boxplot(Data(:,4:6),'notch','on','Colors',"b",'Positions',X2);xticklabels({'1.5mm A','1.5mm B','1.5mm C'})
boxplot(Data(:,7:9),'notch','on','Colors',"g",'Positions',X3);xticklabels({'3mm A','3mm B','3mm C'})
xlim([0 10]);
set(gca,'FontSize',10,'XTickLabelRotation',90)
Try the above again
Hannah Taylor
Hannah Taylor on 24 Feb 2021
still no luck! Looks like the first image again

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!