Reputation: 41
I am using the following code.
days = 1:1:15;
nash=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15];
rew = ones(1,15);
rate = rew/max(rew);
[ax, h1, h2] = plotyy(days, nash, days, rate, 'bar', 'plot');
set(get(ax(2), 'Ylabel'), 'String', 'Average Throughput');
set(get(ax(1), 'Ylabel'), 'String', 'Probability of Convergence');
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .2, pos(3) .65])
Xt = days;
set(gca,'XTick',Xt);
algos = ['[3 36 24]';'[18 36 9]';'[33 24 6]';'[33 12 18]';'[36 17 10]';
'[33 20 10]';'[34 24 5]';'[34 12 17]';'[34 20 9]';'[48 12 3]';
'[48 10 5]';'[48 6 9]';'[40 20 3]';'[40 17 6]';'[40 18 5]'];
ax = axis;
axis(axis);
Yl = ax(3:4);
set(gca,'XTickLabel','')
t = text(Xt,Yl(1)*ones(1,length(Xt)),algos(1:length(days),:));
set(t,'HorizontalAlignment','right','VerticalAlignment','top','Rotation',90,'Fontsize',10);
I want to completely remove the original labels on the x axis, however my x tick labels are not completely turning off.
I am using Matlab R2014a.
Upvotes: 1
Views: 251
Reputation: 30047
As gnovice points out, your issue is with the gca
axis object not being the one with all of the labels! I am leaving my answer here because it builds on that solution with a neater method to align labels to your plot items.
Solve the original problem (as gnovice says) by using
[ax, h1, h2] = plotyy(days, nash, days, rate, 'bar', 'plot');
set(ax,'XTickLabel','') % should work!
Output:
However, it seems like you're trying to replace the x tick labels with a text
object, and I'm not sure why. Simply change the labels to be the text labels!
[ax, h1, h2] = plotyy(days, nash, days, rate, 'bar', 'plot');
% NOTE: I've made this a cell array by using curly braces { }
algos = {'[3 36 24]';'[18 36 9]';'[33 24 6]';'[33 12 18]';'[36 17 10]';'[33 20 10]';'[34 24 5]';'[34 12 17]';'[34 20 9]';'[48 12 3]';'[48 10 5]'; '[48 6 9]';'[40 20 3]';'[40 17 6]';'[40 18 5]'};
% Set the text labels as axis labels
set(ax, 'xticklabel', algos);
% Rotate (as of Matlab 2014b)
set(ax, 'xticklabelrotation', 90);
Using 2014a, you will not be able to use xticklabelrotation
. Instead, you may find the Rotate Tick Label function in the File Exchange useful. This function does essentially do the same thing as you are trying, but is neatly packaged and well reviewed.
Once downloaded, this would simply be called instead of the last line above by
rotateticklabel(ax, 90); % Could even just do rotateticklabel(gca) as 90 is default
Output using the text as labels like shown in my code:
Upvotes: 0
Reputation: 125854
I was able to reproduce your error in R2013a. It turns out that your line:
set(gca,'XTickLabel','')
only removes some of your x axis labels. If you change this to:
set(ax,'XTickLabel','')
and move it up to before you redefine ax
, so that the ax
it refers to is the one returned by plotyy
, it will remove all of the x axis labels for you.
This comes from the fact that there are two axes created by plotyy
, but gca
will only ever refer to one. You can see that both axes have values set for their 'XTickLabel'
property by doing the following after your call to plotyy
:
>> get(ax(1),'XTickLabel') % Get x tick labels for the first axes
ans =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>> get(ax(2),'XTickLabel') % Get x tick labels for the second axes
ans =
0
2
4
6
8
10
12
14
16
Using gca
to change the x tick labels will only change one axes, not both. Hence, you should use the ax
output from plotyy
to modify both axes simultaneously.
NOTE: As of R2016a plotyy
is no longer recommended, and you should instead use yyaxis
.
Upvotes: 1