Ytick label is repeated and not alligend correclty

2 ビュー (過去 30 日間)
Jeyoung Kim
Jeyoung Kim 2023 年 9 月 19 日
編集済み: Star Strider 2023 年 9 月 19 日
I created subplots.
When I use ytick, everything is okay,
x1=subplot(4,1,1);
plot(x,Mode1_BSFC,'-o',Color='k',LineWidth=1,MarkerFaceColor=[0 0 0]);
hold on;
plot(x,Mode2_BSFC,'-^',Color='g',LineWidth=1,MarkerFaceColor=[0 1 0]);
hold on;
plot(x,Mode3_BSFC,'-squar',Color='r',LineWidth=1, MarkerFaceColor=[1 0 0],MarkerSize=9);
xpos = -89 % to align ylabel
xlim([-80 80]);
ylim([-5 20]);
yticks(-5:5:20)
When I increase fontsize of yticklabel as below.
ax1=subplot(4,1,1);
plot(x,Mode1_BSFC,'-o',Color='k',LineWidth=1,MarkerFaceColor=[0 0 0]);
hold on;
plot(x,Mode2_BSFC,'-^',Color='g',LineWidth=1,MarkerFaceColor=[0 1 0]);
hold on;
plot(x,Mode3_BSFC,'-squar',Color='r',LineWidth=1, MarkerFaceColor=[1 0 0],MarkerSize=9);
xpos = -89 % to align ylabel
xlim([-80 80]);
ylim([-5 20]);
yticks(-5:5:20)
set(gca, 'YTick', -5:5:20, 'YTickLabels', [-5 0 5 10 15 20])
set(gca,'YTickLabel',get(gca,'YTickLabel'),'fontsize',9)
Ytick is aligend to left side. I still prefer to aligh on the right side in the first figure.
When I use set(gca,'YTickLabel',get(gca,'YTickLabel'),'fontsize',9), yticks(-5:5:20) does not work,
Some thimg ytick was repeated.
Is there any way to increase ytick font size without using set(gca,'YTickLabel',get(gca,'YTickLabel'),'fontsize',9) like the first code?
Or Is there any way to allign yticklabel to right side as firsth graph using the second code?

回答 (2 件)

Dave B
Dave B 2023 年 9 月 19 日
編集済み: Dave B 2023 年 9 月 19 日
The reason the YTickLabel is aligned to the left is because MATLAB padded the values with spaces to make a character array. This is leftover behavior from the old days when MATLAB didn't have a great string datatype to work with.
plot(rand(10,1))
set(gca,'YTick',0:.1:1,'YTickLabel',0:.1:1)
get(gca,'YTickLabel') % note the spaces in the array
ans = 11×3 char array
'0 ' '0.1' '0.2' '0.3' '0.4' '0.5' '0.6' '0.7' '0.8' '0.9' '1 '
An easy workaround is to just wrap those numbers in a call to string
figure
plot(rand(10,1))
set(gca,'YTick',0:.1:1,'YTickLabel',string(0:.1:1))
get(gca,'YTickLabel') % note it's no a cell array, without a need to have each ticklabel be the same width
ans = 11×1 cell array
{'0' } {'0.1'} {'0.2'} {'0.3'} {'0.4'} {'0.5'} {'0.6'} {'0.7'} {'0.8'} {'0.9'} {'1' }

Star Strider
Star Strider 2023 年 9 月 19 日
編集済み: Star Strider 2023 年 9 月 19 日
The tick labels are text objects, so it should be possible to change their alignments. (I cannot find that property documented anywhere, and experiments with setting it fail.)
x = 1:0.5:10;
y = sin(2*pi*x/10);
figure
subplot(2,1,1)
plot(x, y)
set(gca,'YTick',-1:0.4:1)
ytx = get(gca,'YTick');
set(gca,'YTickLabel',[])
text(ones(1,6)*(min(xlim)-0.05*diff(xlim)), ytx, compose('%-3g',[-5 0 5 10 15 20]))
subplot(2,1,2)
plot(x, y)
set(gca,'YTick',-1:0.4:1)
ytx = get(gca,'YTick');
set(gca,'YTickLabel',[])
text(ones(1,6)*(min(xlim)-0.05*diff(xlim)), ytx, compose('%3g',[-5 0 5 10 15 20]))
The next best option is to set them yourself, as I did here. You can also specify 'HorizontalAlignment','left' or , 'HorizontalAlignment','right' as text arguments.
EDIT — Corrected typographical errors.
.

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by