
Change grid extent so title is visible
7 views (last 30 days)
Show older comments
I'm try to put a title ~7 units above the y-extent of the graph, but as seen here, the title goes off the edge of the grid extent:

How can I change my code to manually ensure the grid extent includes the title? Thanks so much in advance! Here's my code:
figure
x = [4.75 2 0.85 0.425 0.25 0.15 0.075];
y = [1 14 28 50 60 95 100];
semilogx(x,y,'-o');
no = {'#1'; '#2'; '#3'; '#4'; '#5'; '#60'; '#700'};
for m = 1:7
plot([x x],[0 100],'k--')
g = text(x,102,no(m));
set(g,'Rotation',90);
hold on
end
title_hs = title('practice')
set(title_hs,'position',[0.7746 107 0]);
0 Comments
Answers (3)
Star Strider
on 4 Sep 2020
One option is to change the Position prop[erty of the axes:
Ax = gca;
AxP = Ax.Position;
Ax.Position = AxP+[0 0 0 -0.05];
See if that does what you want.
Adding this code to the end of the code you posted produces this result —

0 Comments
Adam Danz
on 4 Sep 2020
Edited: Adam Danz
on 4 Sep 2020
To vertically adjust the axis so the final title position is approximately where it would be from the top of the figure before repositioning it, scale the axes' outerposition relative to the title's vertical displacement.
% Create demo figure
figure
x = [4.75 2 0.85 0.425 0.25 0.15 0.075];
y = [1 14 28 50 60 95 100];
semilogx(x,y,'-o');
title_hs = title('practice');
% Shift title upward
set(title_hs,'position',[0.7746 107 0]);
% Scale the axis outerposition vertically in proportion to the displacement
title_hs.Units = 'Normalize';
ax = gca();
ax.OuterPosition(4) = 1/title_hs.Extent(2);

0 Comments
Sulaymon Eshkabilov
on 4 Sep 2020
Hi,
you'd need to make the following two changes in your scripts for plot figure setting adjustments.
figure
ax1 = axes('Position',[0.13 0.10 0.70 0.70]); % Change 1
...
title_hs = title('practice')
%set(title_hs,'position',[0.7746 107 0]);
set(get(gca,'title'),'Position',[.345 111 0]) % Change 2
1 Comment
Adam Danz
on 4 Sep 2020
Why not just use the title handle title_hs ?
Otherwise, this answer isn't much different than Star Striders'.
See Also
Categories
Find more on Title 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!