how to fit ylabel for the plots?

Hi everyone,
my y label text is kind of hidden when i run the plot, is there a way to solve this and make the y label fitted.
as you can see from the below photo, I can see my y label comeletly when i pull the figure 4 from right side,
I would like to solve this problem without my manual inertact and without changing the fontsize for the label.
thank you.
I use the below commands for my y label
ylabel({'Twist';'Span';'(degrees)'},'fontweight','bold','fontsize',6);
ylh = get(gca,'ylabel');
gyl = get(ylh);
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',0, 'Position',ylp,'VerticalAlignment','middle', 'HorizontalAlignment','right')

1 Comment

The easiest way would be to not override the rotation of the label. Its common for y axis labels to be rotated by 90 degress. Cases like this are one of the big reasons why.

Sign in to comment.

Answers (2)

One possibility —
left_offset = 0.01;
set(ylh, 'Rotation',0, 'Position',ylp+[left_offset 0],'VerticalAlignment','middle', 'HorizontalAlignment','right')
then experiment with the ‘left_offset’ value until it produces the desired result.
.

4 Comments

When i use 0 within the brackets, the y label text is being vertical not horizontal.
when i remove 0, it is working but still the same problem that i asked
The Position result ‘ylp’ should be a 3 element vector (I should have specified ‘left_offset’ as such, that was a typographical error).
With that correction, the approach works —
x = 1:20;
y = rand(size(x));
figure
plot(x, y)
grid
ylabel({'Twist';'Span';'(degrees)'},'fontweight','bold','fontsize',6);
ylh = get(gca,'ylabel');
gyl = get(ylh);
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',0, 'Position',ylp,'VerticalAlignment','middle', 'HorizontalAlignment','right')
figure
plot(x, y)
grid
ylabel({'Twist';'Span';'(degrees)'},'fontweight','bold','fontsize',6);
ylh = get(gca,'ylabel');
gyl = get(ylh);
ylp = get(ylh, 'Position');
left_offset = 3.5;
set(ylh, 'Rotation',0, 'Position',ylp+[left_offset 0 0],'VerticalAlignment','middle', 'HorizontalAlignment','right')
I used a large value for ‘left_offset’ here to illustrate the effect. Change it as necessary to get the desired result.
.
still there is missing actually, now with three element vector is working,
When i use positive offset it is overwritting the axis digits,
I have to make it negative but the gray area is not expanding.
It may be necessary to experiment with the Position properties of the axes, figure, or both to get the desired result. See Graphics Object Properties for links to all that information. It is not straightforward, however it is definitely possible.

Sign in to comment.

You may also experiment with the InnerPosition of 'gca'.
Following shows an example:
Nz = 100;
A = randi(100,1,Nz);
figure(1)
for k = 1:2
subplot(2,1,k)
plot(1:Nz,A);
grid on;
ylabel({'Twist';'Span';'(degrees)'},'fontweight','bold','fontsize',12); % Font Size = 12
ax = gca;
ylh = ax.YLabel;
ylh.Units='normalized';
ylp = ax.YLabel.Position;
set(ylh, 'Rotation',0,'Position',ylp,'VerticalAlignment','middle', 'HorizontalAlignment','right');
if k == 1
title('No shift');
elseif k == 2
totalwidth = abs(ylh.Extent(1))+abs(ylh.Extent(3)); % Get the total width of the label
Diff = abs(ax.InnerPosition(1)-totalwidth/2); % Calculate the required shift
ax.InnerPosition(1)=ax.InnerPosition(1)+Diff; % Add the shift to the start Inner-position
ax.InnerPosition(3)=ax.InnerPosition(3)-Diff; % Subtract shift from the Inner-position width
title('With shift');
end
end

Categories

Asked:

on 18 Jan 2022

Answered:

on 19 Jan 2022

Community Treasure Hunt

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

Start Hunting!