Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multi-Line XtickLabels

2,372 views
Skip to first unread message

Bill_C

unread,
Dec 13, 2004, 4:11:37 PM12/13/04
to
I would like to produce multi-line tick labels using cell strings or
whatever can get the job done. I tried Help-Solutions but no example
of multiple line tick labels for a bar chart. My labels are a bit
long so I want them each to span a few lines. Any suggestions?
(even tried the sprintf option).

Thanks.

Kelly

unread,
Dec 13, 2004, 4:46:11 PM12/13/04
to
Bill_C wrote:

> I would like to produce multi-line tick labels using cell strings
> or

> whatever can get the job done... My labels are a bit


> long so I want them each to span a few lines.

There may be a better way, but I would get rid of the xlabels (assign
them empty strings) and just use the text function. You can play
around with alignment and rotation depending on the amount of text
you need to fit in a given space.

bar(rand(10,1));
set(gca, 'XTick', [1:10], 'XTickLabel', cell(10,1), 'YLim', [0 1]);
text(1, -.05, {'Label # 1'}, 'FontSize', 8, 'HorizontalAlignment',
'Center');
text(5, -.05, {'Label #5', 'has two lines'}, 'FontSize', 8,
'HorizontalAlignment', 'Center');

Stuart McGarrity

unread,
Dec 13, 2004, 4:51:51 PM12/13/04
to
See sprintf to make strings with \n characters in them

"Bill_C" <wm...@gm.com> wrote in message news:eef5e...@webx.raydaftYaTP...

Kelly

unread,
Dec 13, 2004, 5:05:30 PM12/13/04
to
Stuart McGarrity wrote:

> See sprintf to make strings with \n characters in them

I've tried this before, and although I have no problem creating a
multi-line string using sprintf, said string does not display
correctly as an axis label. Instead of creating a multi-line label,
the newline character is displayed as a box.

Is this due to some setting on my computer, or is this always how a
multi-line string will display as an tick label?

-Kelly

Fiona

unread,
Sep 20, 2011, 7:05:29 PM9/20/11
to
Kelly <kake...@NOSPAMcomcast.net> wrote in message <eef5...@webx.raydaftYaTP>...
If anyone cares 7 years later, I have this same problem. On the axis label, the newline character is displayed as a box.

Steven_Lord

unread,
Sep 21, 2011, 9:41:21 AM9/21/11
to


"Fiona " <fn...@yahoo.com> wrote in message
news:j5b67o$79l$1...@newscl01ah.mathworks.com...
Can you post a snippet of code that you're trying to use to generate your
labels?

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Andrew Stevens

unread,
Dec 9, 2011, 1:26:07 PM12/9/11
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <j5cpi1$6vm$1...@newscl01ah.mathworks.com>...
Hi Steve,

I was trying to create multi-line tick labels and came across this thread. For instance, say I want to add a specialized ticklabel with the three-letter month on top and the year below. Here is an example that will produce the problem:

%synthetic time-series
x=now:now+365;
y=rand(numel(x),1);
plot(x,y)

%plot
figure
plot(x,y)
datetick('x','mmm')

xt=get(gca,'xtick');
label_old=cellstr(get(gca,'xticklabel'));
dv=datevec(xt);

%create new labels
label_new=cellfun(@(x,y)(sprintf('%s\n%d',x,y)),...
cellstr(get(gca,'xticklabel')),num2cell(dv(:,1)),'un',0);
set(gca,'xticklabel',label_new)

What I get is the month, then a box, and the year afterward. The '\n' shows up as a box in the tick label. Could this have to do with the 'interpreter' property in the axes? I tried to change it but got an error saying it was not an accessible property in the axes class. I am using R2011b.

-Andrew
0 new messages