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 件のコメント
Matt Waller
Matt Waller 2021 年 9 月 8 日
Thank you! This is perfect.

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeGrid 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