Ytick label is repeated and not alligend correclty
7 次查看(过去 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!