Ytick label is repeated and not alligend correclty

6 visualizaciones (últimos 30 días)
Jeyoung Kim
Jeyoung Kim el 19 de Sept. de 2023
Editada: Star Strider el 19 de Sept. de 2023
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?

Respuestas (2)

Dave B
Dave B el 19 de Sept. de 2023
Editada: Dave B el 19 de Sept. de 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
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 el 19 de Sept. de 2023
Editada: Star Strider el 19 de Sept. de 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.
.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by