Ytick label is repeated and not alligend correclty
조회 수: 12 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
답변 (2개)
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
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
댓글 수: 0
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.
.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!