How can I add XTickLabels in a subplot (2014a)

15 views (last 30 days)
I am having difficulty applying XTickLabels to a subplot. Many examples I find online do not seem to work or give errors. By moving around when the axes are defined I can get the TickLabels on a normal figure, but in a subplot the same order doesn't give labels AND messes up the other labels. Any thoughts?
%%title and labels ok, no tick labels
xlabel('x');
ylabel('y');
title('subplot (1,1)');
axes('XTickLabel',{'a','b','c'});
%%everything ok
axes('XTickLabel',{'a','b','c'});
xlabel('x');
ylabel('y');
title('subplot (1,1)');
%%title and labels ok, no tick labels on subplot1
subplot(3,3,1);
xlabel('x');
ylabel('y');
title('subplot (1,1)');
axes('XTickLabel',{'a','b','c'});
subplot(3,3,2);
xlabel('x');
ylabel('y');
title('subplot (1,2)');
%%No labels for subplot 1, labels ok for subplot 2
subplot(3,3,1);
axes('XTickLabel',{'a','b','c'});
xlabel('x');
ylabel('y');
title('subplot (1,1)');
subplot(3,3,2);
xlabel('x');
ylabel('y');
title('subplot (1,2)');

Accepted Answer

Geoff Hayes
Geoff Hayes on 4 Jun 2014
I think that part of the problem is that when the code calls axes or xlabel it applies the ticks or labels to the current axis which may not be the one corresponding to your subplot. I took the middle chunk of code from above (starting at %% title and labels ok, no tick labels on subplot1) and put it in a test.m file and stepped through each line and observed the changes to the subplots as the line was executed. It may be interesting to do the same.
Now in order to apply the ticks and labels to your choice of subplot, you need the handle to the axis of that subplot. That is easy:
h = subplot(3,3,1);
Then to set the title, labels and ticks, do something like
title(h,'subplot (1,1)');
xlabel(h,'x');
ylabel(h,'y');
set(h,'XTickLabel',{'a','b','c'});
And repeat for the other subplots. Try the above and see what happens!
  1 Comment
Brent Wyk
Brent Wyk on 5 Jun 2014
Thanks, that works and helped me understand what was going wrong

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!