
How is it possible that Matlab still hasn't created a built in function that automatically inserts a title above a group of subplots?
77 views (last 30 days)
Show older comments
It is 2017, and there still is no easy single command built in function that inserts a title above a group of subplots, even though a Million of users keep asking how to do that. How is that possible?
0 Comments
Answers (2)
Cedric Wannaz
on 25 Sep 2017
When you start getting picky, you don't use SUBPLOT anymore because it is not that flexible. Instead you create an axes object that covers the full extent and use it for global titles/legends/etc, and you place your other axes by computing their position.
figure( 'Units', 'normalized', 'Position', [0.2, 0.2, 0.6, 0.6], ...
'Color', 'white' ) ;
% - Bg axes and main title.
bgAxes = axes( 'Position', [0, 0, 1, 1], 'XColor', 'none', 'YColor', 'none', ...
'XLim', [0, 1], 'YLim', [0, 1] ) ;
text( 0.5, 0.95, 'A Small Example', 'FontSize', 16, ...
'HorizontalAlignment', 'center', 'FontWeight', 'bold' ) ;
% - Positions.
x = linspace( 0.05, 0.6, 4 ) ;
w = 0.8 * diff( x(1:2) ) ;
y = linspace( 0.07, 0.7, 3 ) ;
h = 0.8 * diff( y(1:2) ) ;
% - Headers for array still in bgAxes.
for colId = 1 : numel( x )-1
text( x(colId)+w/2, 0.03, sprintf( 'Label X%d', colId ), ...
'HorizontalAlignment', 'center', 'FontWeight', 'bold' ) ;
end
for rowId = 1 : numel( y )-1
text( 0.02, y(rowId)+h/2, sprintf( 'Label Y%d', rowId ), ...
'HorizontalAlignment', 'center', 'Rotation', 90, 'FontWeight', 'bold' ) ;
end
% - Array of plots.
for colId = 1 : numel( x )-1
for rowId = 1 : numel( y )-1
axes( 'Position', [x(colId), y(rowId), w, h] ) ;
plot( sin( rand(1) * (1:10)), 'b' ) ;
grid( 'on' ) ;
end
end
% - Surface.
axes( 'Position', [0.65, 0.45, 0.3, 0.45] ) ;
[X, Y] = meshgrid( -5: .5 : 5 ) ;
Z = Y.*sin(X) - X.*cos(Y) ;
s = surf(X,Y,Z,'FaceAlpha',0.5) ;
s.EdgeColor = 'none';
% - Time series
axes( 'Position', [0.05, 0.72, 0.515, 0.15] ) ;
x = linspace( 0, 10, 100 ) ;
plot( rand(size(x)) + 3 * sin(x) .* exp(-x/5), 'r' ) ;
xlabel( 't [s]' ) ;
ylabel( 'A [V]' ) ;
grid( 'on' ) ;
% - Barchart.
axes( 'Position', [0.65, 0.05, 0.3, 0.3] ) ;
histogram( randn(1000, 1) ) ;
set( gca, 'Box', 'off' ) ;

3 Comments
Jan
on 25 Sep 2017
Edited: Jan
on 26 Sep 2017
Calm down.
I'm using text() or uicontrol('Style', 'Text') for this purpose. This is fast and easy. I'm not one of your million of users.
uicontrol('Style', 'Text', 'Units', 'Normalized', ...
'Position', [0.3, 0.95, 0.4,0.05], ...
'String', 'Super Title')
[EDITED] If want a function:
function H = SubTitle(String, varargin)
H = uicontrol('Style', 'Text', 'Units', 'Normalized', ...
'Position', [0.3, 0.95, 0.4,0.05], ...
'String', String, ...
varargin{:});
end
Now you can append all parameter/value pairs, which are accepted by the uicontrol command: Other positions, font size, etc.
3 Comments
See Also
Categories
Find more on 2-D and 3-D Plots 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!