changing YLabel position and outerposition

If YLabel position change that outerposition mode of axes don't work for YLabel.
It's correct?
figure;
ax1 = axes('OuterPosition',[0 0.50 1.0 0.50]);
ax1.ActivePositionProperty = 'outerposition';
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.Rotation = 0;
ax1.YLabel.String = 'Preserve OuterPosition';
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
outerpos = ax1.OuterPosition;
ti = ax1.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax1.Position = [left bottom ax_width ax_height];

1 Comment

Eugene Paymurzov's answer moved here as a comment.
https://www.mathworks.com/help/matlab/creating_plots/automatic-axes-resize.html are used ActivePositionProperty which is not recommended starting in R2020a
But I think ActivePositionProperty is similar PositionConstraint. I changed ActivePositionProperty to PositionConstraint but the problem didn't gone.
The description of PositionConstraint says:
Position property to hold constant when adding, removing, or changing decorations, specified as one of the following values:
  • 'outerposition' — The OuterPosition property remains constant when you add, remove, or change decorations such as a title or an axis label. If any positional adjustments are needed, MATLAB adjusts the InnerPosition property.
  • 'innerposition' — The InnerPosition property remains constant when you add, remove, or change decorations such as a title or an axis label. If any positional adjustments are needed, MATLAB adjusts the OuterPosition property.
So when I setup PositionConstraint to 'outerposition' I can change property Position of YLabel.
So matlab's help permit me to use my code but Matlab work invalid.
The below picture are shown the figure before changing position of YLabel. PositionConstraint set by 'outerposition' and Matlab work right.
The below picture are shown the figure after changing position of YLabel. PositionConstraint set by 'outerposition' and Matlab work invalid.
The below picture are shown the figure after changing position of YLabel if Matlab would be right

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 5 Jun 2020
Edited: Adam Danz on 17 Mar 2023
Source of the problem (scroll down for solution)
TL;DR: Changing the position of the y-axis label triggers an axis resize that doesn't account for rotated y-axis labels.
Here's a demo similar to yours that illustrates the problem.
1) Create an axes with a y-axis label in its default orientation. Draw a red rectangle around the OuterPosition of the axes and a green rectangle around the Position/InnerPosition of the axes.
figure('color', 'w', 'MenuBar', 'none');
ax1 = axes('OuterPosition',[.25 0.40 .7 0.50]);
ax1.ActivePositionProperty = 'outerposition';
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.String = 'Preserve OuterPosition';
ah(1) = annotation('rectangle',ax1.OuterPosition,'Color', 'r','LineWidth',2);
ah(2) = annotation('rectangle',ax1.Position,'Color','g','LineWidth',2);
2) Rotate the y-axis label and set alignment. Draw blue dashed rectangles showing the updated position properties.
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
ah(3) = annotation('rectangle',ax1.OuterPosition, 'Color', 'b', 'LineStyle','--','LineWidth',2);
ah(4) = annotation('rectangle',ax1.Position, 'Color', 'b','lineStyle','--','LineWidth',2);
As you can see, the OuterPosition propery is preserved and the Position/InnerPosition properties have been adapted to the rotated y-axis label.
3) Change the vertical position of the y-axis label. Draw black dotted rectangles showing the updated position properties.
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line
ah(5) = annotation('rectangle',ax1.OuterPosition, 'Color', 'k', 'LineStyle',':','LineWidth',3);
ah(6) = annotation('rectangle',ax1.Position, 'Color', 'k','lineStyle',':','LineWidth',3);
As you can see, the original position property values have been returned as if the y-axis label were still oriented at 90 degrees.
Solution to the problem
Record the Position value of the axes prior to changing the y-axis label position. After changing the y-axis label position, reset the axes to its original position.
This is applied to the code from your demo.
figure;
ax1 = axes('OuterPosition',[0 0.50 1.0 0.50]);
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.String = 'Preserve OuterPosition';
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
prePosition = ax1.Position; % RECORD THE POSITION OF THE AXES PRIOR TO LABEL POSITION CHANGE
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line
ax1.Position = prePosition; % RESET THE AXIS POSITION
Update
Starting in MATLAB R2023a when you change the Rotation property of an axis label in a 2-D plot, the HorizontalAlignment and the VerticalAlignment properties of the label automatically update to prevent overlap between the label and the axes.

7 Comments

I don't think your solution is right. My first code include the code from Save Plot with Minimal White Space
So that code stopped to work correct.
figure;
ax1 = axes('OuterPosition',[0 0.50 1.0 0.50]);
plot(ax1,0:10,0:10);
ax1.Title.String = 'Preserve OuterPosition';
ax1.YLabel.String = 'Preserve OuterPosition';
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
prePosition = ax1.Position; % RECORD THE POSITION OF THE AXES PRIOR TO LABEL POSITION CHANGE
ax1.YLabel.Position(2) = ax1.YLabel.Position(2)+2; % outerposition mode of axes don't work for YLabel after the line
ax1.Position = prePosition; % RESET THE AXIS POSITION
h(5) = annotation('rectangle',ax1.OuterPosition, 'Color', 'k', 'LineStyle',':','LineWidth',3); % we can see that YLabel lost connection with axes
h(6) = annotation('rectangle',ax1.Position, 'Color', 'k','lineStyle',':','LineWidth',3); % we can see that YLabel lost connection with axes
The below picture are shown that YLabel lost connection with axes
outerpos = ax1.OuterPosition;
ti = ax1.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax1.Position = [left bottom ax_width ax_height]; % invalid work
The below picture are shown the figure after trying to save plot with minimal white space changing position of YLabel. So we see Matlab work invalid
I don't think you read my full answer. The 2nd line of text in my answer tells you what the problem is.
Re-read my answer to understand why rotating the y-label back to 90 deg in your minimal whitespace example will show a figure that fully contains the y-axis label.
ax1.YLabel.Rotation = 90;
In your minimal whitespace example, you're not setting the position back to the "prePosition" so you're not even implementing my solution in that demo. You can't have minimal whitespace and a long, rotated y-axis label at the same time and my answer explains why.
If you have any further questions about what's causing the problem I'd be glad to help.
So I cann't publish my research report because Matlab didn't work correct as Matlab's Help claims.
I change rotations and positions of Label because the standarts of my country demand it. Also I want to use plot with minimal white space
I understand that. But how can you expect to have a long, horizontal y-axis label without having whitespace under and above it? I can't even imagine what that would look like.
It's example. I copied it from help. I want to out the YLabel like 'U, pu' above YLim. Than I want to get plot with minimal white space
figure;
ax1 = axes('OuterPosition',[0 0.50 1.0 0.50]);
plot(ax1,0:10,0:10);
ax1.YLabel.String = 'U, pu';
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
prePosition = ax1.Position; % RECORD THE POSITION OF THE AXES PRIOR TO LABEL POSITION CHANGE
ax1.YLabel.Position(2) = ax1.YLim(2)+1; % outerposition mode of axes don't work for YLabel after the line
ax1.Position = prePosition; % RESET THE AXIS POSITION
ax1 = axes('OuterPosition',[0 0.00 1.0 0.50]);
plot(ax1,0:10,0:10);
ax1.YLabel.String = '\omega, pu';
ax1.YLabel.Rotation = 0;
ax1.YLabel.HorizontalAlignment = 'right';
ax1.YLabel.VerticalAlignment = 'top';
prePosition = ax1.Position; % RECORD THE POSITION OF THE AXES PRIOR TO LABEL POSITION CHANGE
ax1.YLabel.Position(2) = ax1.YLim(2)+1; % outerposition mode of axes don't work for YLabel after the line
ax1.Position = prePosition; % RESET THE AXIS POSITION
I want that the position of YLabel was above YLim. The string of YLabel would be short like 'I, pu' , 'U, pu' , '\omega, pu'.
set(ax1.position,'Units','Normalized')
this snippet makes faster the code

Sign in to comment.

More Answers (4)

Eugene Paymurzov
Eugene Paymurzov on 5 Jun 2020
Edited: Eugene Paymurzov on 5 Jun 2020
Thank Adam Danz.
I read again and understood. I'm newbie for bug report. My bug report send my country distributor and they cann't help me but I cann't find my bug report in bug database. Will the problem fix in the future? I don't understand.

1 Comment

Not all reported bugs are public.
To see a list of bugs you reported, go to your account page (requires that you're logged in).
Then click "Service Requests" under your profile avatar.

Sign in to comment.

Now I will save my figure without Labels as svg file than I will insert into external graph editors for postedit and than the picture will go research report or arcticle.

4 Comments

That's a standard practice in my field. All of my published figures are created in Matlab. Then I transfer the data to OriginLab and create vectorized plots but without labels. Then I transfer the OriginLab image to CorelDraw where I had the labels. It's a giant pain in the butt but it results in more flexibility.
Thank you for advise. I will create figure for exact size of paper than I will save it and will open it in Visio and will insert Math symbols and text.
Do you use Tick of Matlab or insert Tick by using external graph editor so your fonts are equal?
Adam Danz
Adam Danz on 5 Jun 2020
Edited: Adam Danz on 5 Jun 2020
Definitely use the Matlab ticks unless you're recreating the plot within another program. Never separate the axes ticks from the plot that produced them.
I want to try your way to prepare figures for publushing.
How do you transfer the Matlab's figure to OriginLab?

Sign in to comment.

I got the letter.
If an answer helped to resolve your question, thank your answerers by accepting or voting for their answer.
What will I do this question?
My problem concern also XLabel.
Answers helped me that it's the bug of Matlab.
So my question isn't solved. Am I right?

1 Comment

My answer clearly explains the cause of the problem and it provides a solution. Your original question also continued to develop into other questions. The email you got is a reminder to accept answers that were helpful so you can think the volunteers who have give their time to you.

Sign in to comment.

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!