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

Matlab problem: How to put the Xticklabels in between the Xticks!????

2,503 views
Skip to first unread message

serval

unread,
Jul 19, 2006, 4:03:04 PM7/19/06
to
My question is very simple: How to put the "XtickLabels" (for example
months: Jan, Feb, Mar, etc.) in between two "Xticks"! (using "grid
on" for the "Xticks"). That is, put the xlabels in diferent position
than the xticks.

For example, if we have a time series of 365 values, I would like to
obtain a plot with "13 Xticks" and "12 Xticklables": each Xticklabel
should be in between two Xticks (for example the Xticklabel "January"
should be in between the Xtick "1" and the Xtick "31"). I've tried
with:

%========================================
close all
clear all

monthlim=[1,31,59,90,120,151,181,212,243,273,304,334,365]; %13
Xticks.
xticklabels=['J';'F';'M';'A';'M';'J';'J';'A';'S';'O';'N';'D']; %12
XtickLabels.

y=rand(1,365);
plot([1:365],y)
set(gca,'Xlim',[1,365],'XTick',monthlim,'XTicklabel',xticklabels)
grid on
%========================================

...But this can never work since I am using the same position for the
"Xticks" and for the "Xticklabels".

Anybody know how to resolve this problem?.

I would realy apreciate any help or sugestion.

Than you for your time!

Serval.

helper

unread,
Jul 19, 2006, 4:17:47 PM7/19/06
to

You are going to have to use textbox uicontrols and place them where
you want. Check out the following file on the FEX:

<http://www.mathworks.com/matlabcentral/files/8722/rotateticklabel.m>

It is a good example of how to replace labels with text boxes.

serval

unread,
Jul 19, 2006, 4:34:08 PM7/19/06
to
Thank you for your rapid answer.

I remember have seen a web page explaining how to do it without using
"text" command, but I can not find it again...

It is strange anyway that this feature is not included as an option
in Matlab.

Cheers,

Serval.

.........................

helper

unread,
Jul 19, 2006, 4:48:04 PM7/19/06
to
> It is strange anyway that this feature is not included
> as an option in Matlab.

I get the impression they are adding things as fast as they can.

Contact the tech support, and request an enhancement to allow placing
ticklabels at locations other than the ticks.

<http://www.mathworks.com/support/service_requests/contact_support.do>

This will show them that there is at least one customer who is
interested in this functionality.

serval

unread,
Jul 20, 2006, 7:02:18 AM7/20/06
to
Good idea. I just have do it.

See You.

S.

..........................................

serval

unread,
Jul 20, 2006, 7:40:03 AM7/20/06
to
Hi helper. Finally I found a way to solve the problem following the
suggestions of Ri. The solution is:

%=============================================


close all
clear all
monthlim=[1,31,59,90,120,151,181,212,243,273,304,334,365];

monthlim2=0.5*diff(monthlim)+monthlim(1:end-1);


xticklabels=['J';'F';'M';'A';'M';'J';'J';'A';'S';'O';'N';'D'];

y=rand(1,365);

figure(1)
ax(1)=newplot;
set(gcf,'nextplot','add');
set(ax(1),'Xlim',[1,365],'XTick',monthlim2,'XTicklabel',xticklabels);
ax(2)=axes('position',get(ax(1),'position'),'Visible', 'off');
plot(1:365,y)
set(ax(2),'Xlim',[1,365],'XTick',monthlim,'XTicklabel','');
grid on
%=============================================

Serval.

...............................

helper

unread,
Jul 20, 2006, 2:28:42 PM7/20/06
to
serval wrote:
>
>
> Hi helper. Finally I found a way to solve the problem following the
> suggestions of Ri.

Ah, good idea. Use the labels of a second axis underneath the first.

Marcello Vichi

unread,
Feb 19, 2008, 7:51:04 AM2/19/08
to
Maybe now it's a little late for this post, but I've made a
function based on your idea. By the way, many thanks, it
solved me a lot of problems!

It now works on an already existing axis

cheers

marcello

function ax2 = ticklabel(ax1)
% TICKLABEL Shift the tick labels in the X axis
% TICKLABEL(AX) shifts current ticklabels to the right
between
% tick marks in axis AX. It only works for the X axis.
% Returns the handler to the hidden axis with the
centered ticklabels.

% Author M. Vichi (CMCC), from ideas posted on the Mathworks
forum

if nargin==0, ax1=gca; end

ax2=axes('position',get(ax1,'position'));

%invert the order of the axes
c=get(gcf,'children');
set(gcf,'children',flipud(c))

xlim=get(ax1,'XLim');
xtick=get(ax1,'XTick');
delt=diff(xtick);
xtick2=[0.5*[delt delt(end)]+xtick(1:end)];
xticklabels=get(ax1,'XTickLabel');
set(ax2,'Xlim',xlim,'XTick',xtick2,'XTicklabel',xticklabels);
ytick=get(ax1,'YTick');
yticklabels=get(ax1,'YTickLabel');
set(ax2,'Ylim',ylim,'YTick',ytick,'YTicklabel',yticklabels);
set(ax1,'XTickLabel','','Visible', 'on')

Jonas

unread,
Jun 1, 2012, 8:16:24 PM6/1/12
to
Dear Marcello, your function is wonderful! Cheers, Jonas
0 new messages