How I get y-axis bold & new times roman font when using latex as 'TickLabelInterpreter' in Matlab?

Viewed 75

I am trying to use latex as TickLabelInterprete in Matlab. I want to bold both the x-axis (where I use the x-ticklabel along with latex syntax) and y-axis (nothing used, just normal text). At the same time, I want to set the font of all the texts on the figure to be Times New Roman.

I am attaching my code as follows. It appears to be strange for me, when I do not set the TickLabelInterprete to be latex, everything is fine, I can bold the y-axis easily (both y-label and the number on the y-axis). But when TickLabelInterprete = 'latex', the number on the y-axis will automatically turn back to normal font. Any suggestions?

close all;
data = [1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4];
bar(data)
hold on
grid on
xticks([1 2 3 4])
xtickangle(0)

ax = gca;
ax.TickLabelInterpreter = 'latex';

xTicklabels = {'\textbf{label 1} \boldmath$E_1$', ...
    '\textbf{label 2} \boldmath$E_2$', ...
    '\textbf{label 3} \boldmath$E_3$', ...
    '\textbf{label 4} \boldmath$E_4$'};

set(gca,'XTickLabel',xTicklabels);

xlabel("Different algorithms");
ylabel("Result of schemes");

lgd = legend("1",...
    "2",...
    "3",...
    "4",...
    'Location',"northwest");
lgd.FontWeight = "bold";

h = gca;
h.XAxis.FontWeight = 'bold';
h.XAxis.LineWidth = 1;
h.YAxis.FontWeight = 'bold';
h.YAxis.LineWidth = 1;
set(gca,'fontname','times');


hold off;
1 Answers

You want to set h.YAxis.TickLabelFormat to bold as well. For the case of xTicklabels, you've done it manually; you could do the same for the yTicklabels, but the faster way here is to set the TickLabelFormat since the ticks are already normal numbers and need no customization.

close all;
data = [1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4];
bar(data)
grid on
xticks([1 2 3 4])
xtickangle(0)

ax = gca;
ax.TickLabelInterpreter = 'latex';

xTicklabels = {'\textbf{label 1} \boldmath$E_1$', ...
    '\textbf{label 2} \boldmath$E_2$', ...
    '\textbf{label 3} \boldmath$E_3$', ...
    '\textbf{label 4} \boldmath$E_4$'};

set(gca,'XTickLabel',xTicklabels);

xlabel("Different algorithms");
ylabel("Result of schemes");

lgd = legend("1", "2", "3", "4", 'Location', "northwest");
lgd.FontWeight = "bold";

h = gca;
h.XAxis.FontWeight = 'bold';
h.XAxis.LineWidth = 1;
h.YAxis.FontWeight = 'bold';
h.YAxis.LineWidth = 1;
set(gca,'fontname','times');
h.YAxis.TickLabelFormat = '\\textbf{%g}';   % <---- ADD THIS LINE

enter image description here

Related