How can I insert a title over a group of subplots?

조회 수: 10,914(최근 30일)
MathWorks Support Team
MathWorks Support Team 2010년 4월 26일
댓글: Mirza Faizaan 2021년 9월 27일
I would like to place a title over a group of subplots.  How can I do this?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 3월 15일
편집: MathWorks Support Team 2021년 3월 15일
Starting in R2019b, you can use the tiledlayout and nexttile functions to create a configurable tiling of plots. The configuration options include: 
  • Control over the spacing between the plots and around the edges of the layout 
  • An option for a shared title at the top of the layout 
  • Options for shared x- and y-axis labels 
  • An option to control whether the tiling has a fixed size or variable size that can reflow 
For more information, see Combine Multiple Plots
Starting in R2018b, you can use the sgtitle function to add a title to a group of subplots. For example: 
subplot(2,2,1) 
title('First Subplot') 
subplot(2,2,2) 
title('Second Subplot') 
subplot(2,2,3) 
title('Third Subplot') 
subplot(2,2,4) 
title('Fourth Subplot') 
sgtitle('Subplot Grid Title') 
For more information on the sgtitle function, see: https://www.mathworks.com/help/matlab/ref/sgtitle.html  
In previous releases (before R2018b), you can create the appearance of a super title by creating the subplots in a panel and adding a title to the panel. For an example, see: https://www.mathworks.com/help/releases/R2018a/matlab/creating_plots/combine-multiple-plots.html#d119e2898 
  댓글 수: 9
Mirza Faizaan
Mirza Faizaan 2021년 9월 27일
sgtitle works fine and shows the title in the output window. However, the sgtitle is omitted when figure is opened as a window. Any idea how to fix this?

댓글을 달려면 로그인하십시오.

추가 답변(10개)

jcjaimes
jcjaimes 2016년 12월 15일
Suptitle does the job
x=1:0.01:10;
subplot(2,2,1); plot(sin(x))
subplot(2,2,2); plot(cos(x))
subplot(2,2,3); plot(cos(x.^2))
subplot(2,2,4); plot(sin(x.^2))
suptitle('I am a super title')
  댓글 수: 6
Thiviya Navaneethan
Thiviya Navaneethan 2021년 3월 17일
Hi Sourav,
Please try using sgtitle function, see: https://www.mathworks.com/help/matlab/ref/sgtitle.html
If this does not help, contact MathWorks Technical support. They may be able to assist you further. https://www.mathworks.com/support/contact_us.html

댓글을 달려면 로그인하십시오.


Paul Huxel
Paul Huxel 2018년 10월 9일
Starting in R2018b, you can use sgtitle to add a title to subplot grids:
  댓글 수: 3
LEONG SING YEW
LEONG SING YEW 2020년 3월 8일
Thanks :D

댓글을 달려면 로그인하십시오.


Jonathan Beard
Jonathan Beard 2017년 5월 9일
Here's a solution that doesn't require additional functions.
figure;
subplot(2, 1, 1);
plot(nan, nan);
subplot(2, 1, 2);
plot(nan, nan);
currentFigure = gcf;
title(currentFigure.Children(end), 'blah');
  댓글 수: 7
PROSANJEET SARKAR
PROSANJEET SARKAR 2020년 3월 27일
sgtitile() function you cane used it provide in 2018 verison of matlab

댓글을 달려면 로그인하십시오.


Anders Bertelsen
Anders Bertelsen 2017년 9월 13일
편집: Stephen 2017년 9월 13일
figure;
subplot(1, 2, 1);
plot(nan, nan);
subplot(1, 2, 2);
plot(nan, nan);
ha = axes('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text(0.5, 0.98,'Title')
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 5월 11일
There are multiple possibilities. The ones I can think of off-hand include:
  • create an axes for each of the images, and an axes for each of the titles above and each of the titles to the side. Using subplot() for this purpose is not great, as you do not want the axes to all be the same size. Each axes could been panned, scrolled, zoomed, or data cursored individiually.
  • create the top left axes with room for title and ylabel and an image. Create 3 axes below that with room for ylabel and an image. Create top right axes with room for title and image. Create three axes below that with room for an image. Using subplot() for this purpose is not great, as you do not want the axes to all be the same size. Each axes could been panned, scrolled, zoomed, or data cursored individiually.
  • create a 4 x 2 array of axes the same size, all large enough to accomodate title and ylabel. Then carefully "tuck" the axes in so only the wanted parts show. Using subplot() for this might not be bad, but you will need to move the axes carefully. Each axes could been panned, scrolled, zoomed, or data cursored individiually.
  • Use montage or similar to create a 4 x 2 image array and display it in an axes that you put appropriate text labels on. It would not be possible to scroll or pan or zoom the images individually, and you would need extra work to data cursor them individually. On the other hand, a single movable graphics cursor covering multiple images would become easier than in the other approaches mentioned above

댓글을 달려면 로그인하십시오.


Brad Stiritz
Brad Stiritz 2018년 9월 9일
>Answer by MathWorks Support Team on 26 Apr 2010
>It is not possible to put a title over a group of subplots in MATLAB..
With respect, this is not a helpful comment, as it's apparently not true. It is possible, just not with built-in core Matlab functionality. Most people will hopefully continue reading the thread, and learn about the workarounds. Still, it's not an encouraging or precise statement to begin the group discussion, IMHO.
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2018년 10월 25일
FYI: suptitle is only available to those who have the Bioinformatics toolbox, and then it was included in a demo file and is not an actual MathWorks maintained function.
However, as Paul Huxel points out, there is now (r2018b) a function in base MATLAB for this. It is called sgtitle.

댓글을 달려면 로그인하십시오.


Eric Sargent
Eric Sargent 2020년 12월 9일
편집: Eric Sargent 2020년 12월 9일
Starting in R2019b, you can also use tiledlayout and nexttile instead of subplot, which has shared titles and labels. You can use the title, xlabel, and ylabel commands directly with tiledlayouts:
Alternatively, starting in R2018b, the sgtitle function will add a title over a group of subplots.
tl = tiledlayout(3,3);
for i = 1:9
nexttile(tl)
plot(rand(5,1));
end
title(tl,'Layout Title');
xlabel(tl,'Layout XLabel');
ylabel(tl,'Layout YLabel');

Tahariet Sharon
Tahariet Sharon 2021년 5월 25일
How would you insert multiple lines? \n won't work.
  댓글 수: 2
Miguel D. Martinez
Miguel D. Martinez 2021년 9월 5일
This exactly what i was looking for. subtext for subplot subtitle haha thank you very much !

댓글을 달려면 로그인하십시오.


Dan
Dan 2017년 10월 31일
Thanks! This works great. I set did >set(h,'Position',[.5 1.05 .5]) to get place the supertitle above the titles of my subplots.

JB
JB 2019년 7월 4일
A great big thank you to the writer of mtit! That worked great for me, being someone who is between 2018 and the version I originally wrote my code in where my title for the subplot was centered without having to do anything extra.

Community Treasure Hunt

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

Start Hunting!

Translated by