How to change font type of bar plot labels?

조회 수: 114 (최근 30일)
Matt Waller
Matt Waller 2021년 9월 7일
댓글: Matt Waller 2021년 9월 8일
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