Ytick label is repeated and not alligend correclty
6 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Dave B
on 19 Sep 2023
Edited: Dave B
on 19 Sep 2023
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 Comments
Star Strider
on 19 Sep 2023
Edited: Star Strider
on 19 Sep 2023
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 Comments
See Also
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!