Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different fontsizes for tick labels of x- and y-axis

I would like to have tick labels with different font size on x- and y-axis.

My first try was:

set(gca,'XTickLabel', {labelslist}, 'FontSize',16)

but it does not work, at least on with my version (2014a on Windows10). For some reason it changes the label font size on both axis.

Does anyone know how to do it?

minimal example:

A=[1 2 3; 2 3 4; 2 3 4; 1 1 1];

figure
bar([1:size(A,1)], A, 'BarWidth', 2)
set(gca,'xticklabel',{'1','2','3','4'},'FontSize',16)

like image 632
shamalaia Avatar asked Oct 14 '25 18:10

shamalaia


1 Answers

You need two axes objects on top of each other, one for x and one for y:

%// example figure
A = [1 2 3; 2 3 4; 2 3 4; 1 1 1];
figure
bar([1:size(A,1)], A, 'BarWidth', 1)

%// handle
ax1 = gca;
%// fontsize of y-axis, deactivate, x-axis
set(ax1,'XTick',[],'FontSize',24)
%// create second identical axis and link it to first one
ax2 = axes('Position', get(ax1, 'Position'),'Color','none');
linkaxes([ax1,ax2],'xy')
%// fontsize of x-axis, deactivate, y-axis
set(ax2,'YTick',[],'FontSize',12)

enter image description here


Regarding your comment, don't mix up the handles:

%// handle
ax1 = gca;
%// fontsize of y-axis, deactivate, x-axis
set(ax1,'XTick',[],'YTick',0:4,'YTickLabel',{'ZERO','ONE','TWO','THREE','FOUR'},'FontSize',24)
%// create second identical axis and link it to first one
ax2 = axes('Position', get(ax1, 'Position'),'Color','none');
linkaxes([ax1,ax2],'xy')
%// fontsize of x-axis, deactivate, y-axis
set(ax2,'YTick',[],'XTick',1:4,'XTickLabel',{'one','two','three','four'},'FontSize',12)

enter image description here

like image 139
Robert Seifert Avatar answered Oct 18 '25 04:10

Robert Seifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!