How to change font type of bar plot labels?

115 次查看(过去 30 天)
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 个评论
Chien Poon
Chien Poon 2021-9-7
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 2021-9-8
Possibly. For context, I chose the LaTeX interpreter because I am used to using LaTeX and because the serif font matches my paper better.

请先登录,再进行评论。

采纳的回答

Dave B
Dave B 2021-9-7
编辑:Dave B 2021-9-7
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

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by