boxplot labels from numbers and strings

3 views (last 30 days)
Stefka
Stefka on 5 Nov 2015
Edited: dpb on 5 Nov 2015
Hello, How can I use both a vector of numbers and a string to label different boxplots in one graph? if a, b are vectors then something of this sort doesn't work boxplot([a,b],'labels',[1,'b'])
Thank you, Stefka
  1 Comment
Stefka
Stefka on 5 Nov 2015
Perhaps this example was too simple, but if you have to change the labels you have to type everything again. Suppose you have 8 boxplots in a graph, and you assign labels corresponding to the number of the column of the matrix A by typing boxplot(A(:,1:7),'labels', {'1','2',...'6','emp'}). Now you need to change the boxplots to A(:,5:10) and change the labels to {'5','6'',...'10'}. Is there an easier way than just type again everything? How would num2str(v) work in this respect. I tried several versions with set(gca,'XTickLabel',..) but it doesn't work.

Sign in to comment.

Answers (1)

dpb
dpb on 5 Nov 2015
Edited: dpb on 5 Nov 2015
_"[1,'b']"_ doesn't work outside of *boxplot*, either; you can't juxtapose character and numeric data in a vector. Use cell strings instead...
boxplot([a b],'labels',{'1';'b'})
Or more generically, num2str(v) if v is a value returns the character representation rather than hardcoding it.
ADDENDUM
N=10; % a number of points to illustrate...
a=randn(5,N); % rand data to plot for N observations...
boxplot(a,num2str([1:N].','Obs%2d');
The only "trick" above is to be sure to orient the numeric vector as a column; that's the hint to num2str to put each element on a separate row; otherwise the all get run together on a single line.
Any other of a multitude of ways can be used to generate the label strings as long as end up with either a column character array or a cellstr array.

Community Treasure Hunt

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

Start Hunting!