changing YLabel position and outerposition

85 次查看(过去 30 天)
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 个评论
Adam Danz
Adam Danz 2020-6-5
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

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-6-5
编辑:Adam Danz 2023-3-17
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 个评论
Eugene Paymurzov
Eugene Paymurzov 2020-6-5
编辑:Eugene Paymurzov 2020-6-5
I want that the position of YLabel was above YLim. The string of YLabel would be short like 'I, pu' , 'U, pu' , '\omega, pu'.
YOGA NARASIMHA EPURI
set(ax1.position,'Units','Normalized')
this snippet makes faster the code

请先登录,再进行评论。

更多回答(4 个)

Eugene Paymurzov
Eugene Paymurzov 2020-6-5
编辑:Eugene Paymurzov 2020-6-5
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 个评论
Adam Danz
Adam Danz 2020-6-5
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.

请先登录,再进行评论。


Eugene Paymurzov
Eugene Paymurzov 2020-6-5
My two service requests were gone.

Eugene Paymurzov
Eugene Paymurzov 2020-6-5
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 个评论
Adam Danz
Adam Danz 2020-6-5
编辑:Adam Danz 2020-6-5
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.
Eugene Paymurzov
Eugene Paymurzov 2020-6-8
I want to try your way to prepare figures for publushing.
How do you transfer the Matlab's figure to OriginLab?

请先登录,再进行评论。


Eugene Paymurzov
Eugene Paymurzov 2020-6-8
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 个评论
Adam Danz
Adam Danz 2020-6-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by