Rotating labels and superscript in Matlab

740 views Asked by At

I am creating a bar plot like this: this My problem is that I want to rotate the tick labels (because they are long). I am using Xticklabel_rotate function from the Matlab file exchange for this. Unfortunately this doesn't seem to work with Tex commands for subscript or superscript, and I would also like to add the degree symbol for degrees Celsius. Anyone have a solution for rotation of x-ticks that allows Tex commands in Matlab?

Here is my code:

% Necessary variables:
orderedDriver = {'T21m','Tsoil','prevNEE','vpd','h2osoil'};
tmp1 = {'T21m';'Tsoil';'prevNEE';'vpd';'h2osoil'};
B = [0.373457670910667,0.332391484167831,0.244013230957008,0.209637035240858,0.155440287941303];
r2_benchmark = [0.563879739982401];
month = ['snowmelt'];
fname='Arial';
fsize=22;
fsizeMed=18;

% start figure
scrsz = get(0,'ScreenSize');
figure1 =figure('Position',[1 scrsz(4) scrsz(3)/2 scrsz(4)/2.5]);

% make string of Xtick names
tmp1=orderedDriver(:);
XTL_prim={};
for z = 1:length(tmp1)
    if strcmpi(tmp1(z),'h2osoil')
        XTL_prim{z}='Soil moist. (m^3/m^3)';
    elseif strcmpi(tmp1(z),'Tsoil')
        XTL_prim{z}='Soil temp (C)';
    elseif strcmpi(tmp1(z),'T21m')
        XTL_prim{z}='Air temp. (C)';
    elseif strcmpi(tmp1(z),'vpd')
        XTL_prim{z}='VPD (kPa)';
    elseif strcmpi(tmp1(z),'Rnet25')
        XTL_prim{z}='Net rad. (W/m^2)';
    elseif strcmpi(tmp1(z),'ustar21m')
        XTL_prim{z}='U* (m/s)';
    elseif strcmpi(tmp1(z),'swe')
        XTL_prim{z}='SWE (mm)';
    elseif strcmpi(tmp1(z),'wd21m')
        XTL_prim{z}= 'Wind dir. (deg. from N)';
    elseif strcmpi(tmp1(z),'RH2m')
        XTL_prim{z}='Rel. Humidity';
    elseif strcmpi(tmp1(z),'precipmm')
        XTL_prim{z}='Precip (mm)';
    elseif strcmpi(tmp1(z),'ws21m')
        XTL_prim{z}='Wind speed (m/s)';
    elseif strcmpi(tmp1(z),'Rppfdin')
        XTL_prim{z}='In. PAR (umol/m^2/s)';
    elseif strcmpi(tmp1(z),'prevNEE')
        XTL_prim{z}='Previous NEE'; % ADD PREVIOUS NEE units
    end
end

% Create axes and XTick properties
axes1 = axes('Parent',figure1,...
    'XTickLabel',XTL_prim,...
    'XTick',1:length(orderedDriver),'FontSize',fsizeMed,'Position',[0.2 0.25 0.671 0.659]); %Play         with [x, y, xsize, ysize] to get figure positioned well
hold(axes1,'all');

% hold on
eachH=bar(B);
xlim([0 length(orderedDriver)+1])
ylim([0 1])

% Change bar color
set(eachH,'facecolor',[0.4 0.4 0.4])

% plot the best we can expect from the benchmark
l1=line([0;length(orderedDriver)+1],[r2_benchmark;r2_benchmark]);
set(l1,'LineStyle','--','color','k')

% Rotate labels
xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','none')

% Label axes and add title
ylabel('r^2')
xlabel('Individual Drivers','FontName',fname)
hold off
text(1,0.93,month,...
    'FontSize',fsizeMed); % can use 'title' command or simply text for title
2

There are 2 answers

1
rozsasarpi On BEST ANSWER

I recommend you to use format_tick instead of xticklabel_rotate, the former allows you to format your labels as TeX objects and also has positioning options.

Make the following changes in your code to get the desired output:

Change from:

% Rotate labels
xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','none')

to:

% Rotate labels
format_ticks(gca, XTL_prim, [], [], [], 45)

Add after setting xlabel:

xlabh = get(gca,'XLabel');
set(xlabh,'Position',get(xlabh,'Position') - [0 0.25 0])

The result:

enter image description here

You can specify the position of tick labels in format_tick if you think they are too close to the bars. To add degrees: 'Soil temp (C\circ)'.

0
user2860703 On

I discovered two more solutions. One is to replace

xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','none')

with

xticklabel_rotate([],45,[],'Fontsize',fsizeMed,'interpreter','tex')

(I could have sworn I tried that already!)

The other is to use the newest version of Matlab (R2014b), which has a new command possibility, 'XTickLabelRotation.'

% Create axes and XTick properties
axes1 = axes('Parent',figure1,...
    'XTickLabel',XTL_prim,...
    'XTick',1:length(orderedDriver),...
    'XTickLabelRotation',45,...
    'FontSize',fsizeMed,'Position',[0.2 0.25 0.671 0.659]); %Play with [x, y, xsize, ysize] to get        figure positioned well
hold(axes1,'all');