Search code examples
matlablatexbar-chartmatlab-figure

Greek symbols in labels for bar chart in MATLAB


I would like to use greek symbols in the ylabels for my barh() chart. I tried the following but it didn't really work:

tplot = barh(mdata, 'BarWidth', 0.3);
set(gca,'xgrid','on')
lbl = {'$$\hat{\sigma}_1$$', '$$\hat{\sigma}_2$$', '$$\hat{\sigma}_3$$'};
box off
set(gca,'yticklabel',lbl)
h=findobj(gca,'type','text'); 
set(h,'Interpreter','latex') 

I also tried:

set(gca,'TicklLabelInterpreter', 'tex')

When I do get(gca), the property TickLabelInterpreter doesn't appear to be there at all! The version of MATLAB I am using is R2013a.

Please note that I specifically used latex as interpreter, rather than tex, because tex doesn't support \hat.


Solution

  • According to this quote by the MathWorks:

    The ability to make the Xtick labels and Ytick labels utilize the same font as TEXT objects with LaTeX as their interpreter is not available in MATLAB 8.1 (R2013a).

    Therefore you need to remove the ylabels and create new ones manually as text objects on their own. You can then use the latex interpreter.

    Here is a modified version of their example to fit your purpose:

    clc
    clear
    
    y = [57,91,105];
    tplot = barh(y, 'BarWidth', 0.3);
    
    lbl = {'$$\hat{\sigma}_1$$', '$$\hat{\sigma}_2$$', '$$\hat{\sigma}_3$$'};
    
    %% Generate figure and remove ticklabels
    
    set(gca,'yticklabel',[])
    
    %% Get tick mark positions
    yTicks = get(gca,'ytick');
    
    ax = axis; %Get left most x-position
    
    %% Reset the ytick labels in desired font
    for i = 1:length(yTicks)
    %Create text box and set appropriate properties
         text(ax(1) - .3,yTicks(i),lbl{i},...
             'HorizontalAlignment','Right','interpreter', 'latex','FontSize',18);   
    end
    

    Output:

    enter image description here