How to move title text to front of plot area

172 views (last 30 days)
N. Fernandez
N. Fernandez on 31 Jan 2022
Edited: Arif Hoq on 31 Jan 2022
Hello,
I would like to move the title to inside the ploting area of my figure.
The title appears by default at the top of the plotting area and centered. To change title position I use:
title(['this is my title'])
set(get(gca,'title'),'Position', [x y z])
The problem is that the text of the title appers behind the plotted area. See attached figure. The title is only seen where it is above the plotting area. At the plotting area (which here is seen filled in white) the title is "hidden" behind.
How can I bring the title to the front of the plot area so that the text is visible on top of anything that is inside the plot area (white or any other filling colors)?
Thank you,
  2 Comments
Arif Hoq
Arif Hoq on 31 Jan 2022
Did you try like this ?
x = linspace(0,10,150);
y = sin(x);
figure(1)
plot(x,y,'Color','r')
title('this is my title')
xlabel('x axis')
ylabel('sin(x)')

Sign in to comment.

Answers (2)

Ankit
Ankit on 31 Jan 2022
Edited: Ankit on 31 Jan 2022
plot(rand(1,50));
t = title('this is my title', 'Units', 'normalized', 'Position', [0.5, 0.75, 0]);
t.Color = 'r'; t.FontSize = 10; % with this you can change color, font name and size
Another way to achieve this is with annotation, please see one such example. More Info: Create annotations - MATLAB annotation - MathWorks Deutschland
figure;
plot(rand(1,50));
dim = [.2 .5 .3 .3];
str = 'this is my title';
a = annotation('textbox',dim,'String',str,'FitBoxToText','on');
a.BackgroundColor = 'g'; % make the background color green
a.FaceAlpha = .2; % this will make translucent

Arif Hoq
Arif Hoq on 31 Jan 2022
Edited: Arif Hoq on 31 Jan 2022
this one should work. You have to adjust the position value.
x = linspace(0,10,150);
y = sin(x);
figure(1)
plot(x,y,'Color','r')
% title('this is my title')
xlabel('x axis')
ylabel('sin(x)')
title('this is my title', 'Units', 'normalized', 'Position', [.5, 1, 1]);
Or, try with this
x = linspace(0,10,150);
y = sin(x);
figure(1)
plot(x,y,'Color','r')
title('this is my title')
xlabel('x axis')
ylabel('sin(x)')
set(get(gca,'title'),'Position', [5 1 0])

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by