13

there are many subplots and each subplot has its own title. how can add a title over all of these group of subplots? I want this title to be shown at top center.

x = linspace(-5,5);

y1 = sin(x);
subplot(2,5,[1:2])
plot(x,y1)
title('y=sin(x)')

y2 = cos(x);
subplot(2,5,[3:4])
plot(x,y2)
title('y=cos(x)')

y3 = tan(x);
subplot(2,5,[5,10])
plot(x,y3)
title('y=tan(x)')

y4 = sin(2*x);
subplot(2,5,[6:7])
plot(x,y1)
title('y=sin(2x)')

y5 = cos(2*x);
subplot(2,5,[8:9])
plot(x,y2)
title('y=acos(2x)')
Sadegh
  • 865
  • 1
  • 23
  • 47
  • 1
    You need the command `suptitle`. – Adiel Nov 08 '15 at 12:36
  • 4
    @Adiel, et al.: Note that `suptitle` is in the Bioinformatics toolbox where it's used for demos. For those without this toolbox, there are [several option on the MathWorks File Exchange](http://www.mathworks.com/matlabcentral/fileexchange/?utf8=✓&term=suptitle). – horchler Nov 08 '15 at 19:50
  • 1
    Why Matlab do not make it a built-in function ? it is the most useful function for any user. – Sadegh Nov 09 '15 at 10:33

3 Answers3

11

Since Matlab 2018b, the new function sgtitle adds a title to a subplot group, simply add sgtitle('Subplot Title');. It doesn't need a toolbox.

For example:

subplot(1,2,1)
plot(cos(0:40));
title('cos');

subplot(1,2,2)
plot(sin(0:40))
title('sin');

sgtitle('Trigo');

trigo

alpereira7
  • 1,522
  • 3
  • 17
  • 30
7

The simplest way I have found for people without the bioinformatics toolbox is this:

a = axes;
t = title('My title');
a.Visible = 'off';
t.Visible = 'on';

What you're doing is creating a new set of axes which, by default, covers the whole figure, and creating a title on those axes. Then the axes are made invisible, and this is overridden for the title which is made visible again.

If the resulting title collides with things, fiddle with a.Position to move the axes around.

Yes, it's ridiculous that this isn't part of base functionality, but there are plenty of one- or two-line functions hidden in toolboxes that one might say that about ;-) (looking at you, range.)

Flyto
  • 676
  • 1
  • 7
  • 18
5
x = linspace(-5,5);

y1 = sin(x);
subplot(2,5,[1:2])
plot(x,y1)
title('y=sin(x)')

y2 = cos(x);
subplot(2,5,[3:4])
plot(x,y2)
title('y=cos(x)')

y3 = tan(x);
subplot(2,5,[5,10])
plot(x,y3)
title('y=tan(x)')

y4 = sin(2*x);
subplot(2,5,[6:7])
plot(x,y1)
title('y=sin(2x)')

y5 = cos(2*x);
subplot(2,5,[8:9])
plot(x,y2)
title('y=acos(2x)')

suptitle('my title');
Sadegh
  • 865
  • 1
  • 23
  • 47
  • 3
    What if you don't have the toolbox for `suptitle()`? Any work around? – Austin Jan 28 '17 at 06:13
  • I don't know. Maybe you can ask another question to hopefully have some answer. – Sadegh Jan 28 '17 at 09:24
  • 4
    Please refrain from dumping code-only answers to Stack Overflow. You are not writing answers only to answer the question, but also to be understandable to future readers. I read this now, 3 years after posting, and have no idea what I should be looking at. Please explain your code, either in text around the code, or in comments inside the code. – Adriaan Nov 28 '18 at 13:22