How to change font type of bar plot labels?

106 views (last 30 days)
Hello, I am trying to set the font type of the category labels ("Category 1", "Category 2", etc.) to match that of the y-axis label ("Some Y label") which was made using the latex interpreter. Also, I would like to change font type of the y-axis ticks (0, 20, 40, etc.) to match. Anyone know how to do this? I've included an example of my code and the bar plot it generates. Thank you.
x = 1:5; % 5 bars
str = {'Category 1'; 'Category 2 '; 'Category 3'; 'Category 4'; 'Category 5'};
Data = [57.3 58.6 41.2 20.2 34.2]; % Average values of data from each of the 5 categories
StdDev = [5.5 3.94 4.5 0.37 3.30]; % Standard deviations of data from each of the 5 categories
errhigh = StdDev./2; % define error bars based on standard deviation
errlow = StdDev./2;
b = bar(x,Data,'FaceColor','flat'); % bar plot
b.CData(1,:) = [0 1 0]; % green
b.CData(2,:) = [0 0 1]; % blue
b.CData(3,:) = [1 0 0]; % red
b.CData(4,:) = [0 1 1]; % cyan
b.CData(5,:) = [1 0 1]; % purple
ylim([0 80])
grid on
grid minor
ylabel('Some Y label','interpreter','latex')
set(gca, 'XTickLabel',str, 'XTick',1:numel(str),'FontSize',20)
xtickangle(45)
hold on
er = errorbar(x,Data,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
er.LineWidth = 2.0;
hold off
  2 Comments
Chien Poon
Chien Poon on 7 Sep 2021
wouldn't it be easier to use matlab's interpreter, since it can do most of what latex could? Maybe i'm not seeing the context of this problem.
Matt Waller
Matt Waller on 8 Sep 2021
Possibly. For context, I chose the LaTeX interpreter because I am used to using LaTeX and because the serif font matches my paper better.

Sign in to comment.

Accepted Answer

Dave B
Dave B on 7 Sep 2021
Edited: Dave B on 7 Sep 2021
You can set the X Axis Tick Label Interpreter (wow a mouthful!) as follows:
ax.XAxis.TickLabelInterpreter='latex'
where ax is your axes.
Or if you want to set both (really all three, but the z axis is sort-of irrelevant here) tick label interpreters:
ax.TickLabelInterpreter='latex'
Here's your bar with the change:
x = 1:5; % 5 bars
str = {'Category 1'; 'Category 2 '; 'Category 3'; 'Category 4'; 'Category 5'};
Data = [57.3 58.6 41.2 20.2 34.2]; % Average values of data from each of the 5 categories
StdDev = [5.5 3.94 4.5 0.37 3.30]; % Standard deviations of data from each of the 5 categories
errhigh = StdDev./2; % define error bars based on standard deviation
errlow = StdDev./2;
b = bar(x,Data,'FaceColor','flat'); % bar plot
b.CData(1,:) = [0 1 0]; % green
b.CData(2,:) = [0 0 1]; % blue
b.CData(3,:) = [1 0 0]; % red
b.CData(4,:) = [0 1 1]; % cyan
b.CData(5,:) = [1 0 1]; % purple
ylim([0 80])
grid on
grid minor
ylabel('Some Y label','interpreter','latex')
set(gca, 'XTickLabel',str, 'XTick',1:numel(str),'FontSize',20, 'TickLabelInterpreter', 'latex');
xtickangle(45)
hold on
er = errorbar(x,Data,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
er.LineWidth = 2.0;
hold off

More Answers (1)

dpb
dpb on 7 Sep 2021
...
hAx=gca;
hAx.TickLabelInterpreter='latex';
xticks(1:numel(str))
xticklabels(str)
hAx.FontSize=20;
...

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!