Title over group of subplots

拈花ヽ惹草 提交于 2019-12-01 03:58:47

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');

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.)

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');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!