237 views (last 30 days)
Show older comments
Samuele Bolotta on 15 Feb 2021
Commented: Adam Danz on 7 Aug 2024 at 12:23
Accepted Answer: Adam Danz
- Screenshot from 2021-02-15 14-13-10.png
Open in MATLAB Online
I attached a screenshot of my plot.
I would like to put on the left (instead of above) the titles (Currents at -70mv, -50, -30 and -10) of the four subplots on the left; on the other hand, I would like to put on the right (instead of above) the titles (Currents at -60mv, -40, -20 and 0) of the four subplots on the right. This is to save space and delete some of the blank space between the subplots.
As for the subtitle ("I is 2 times slower than E"), I'd like to have only one instead of eight, possibly above all the subplots in the centre.
This is how I'm doing now (there's another for loop "for rat = 1:colnum2" outside here but it's not relevant for this question):
for vol = 1:colnum
subplot(4,2,vol);
plot(t_plot, currents(:,hh:zz), 'linewidth',2); legend('IPSC', 'EPSC', 'CPSC');
str = sprintf('Currents at %d mV ', Vm(1,vol));
title(str)
subtitle(['I is ', num2str(ratio(rat)), ' times slower than E'])
xlabel('Time(msec)', 'fontsize', 7)
ylabel('Microamperes (uA)', 'fontsize', 7);
hh = hh + niter;
zz = zz + niter;
end
Thanks!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Adam Danz on 15 Feb 2021
Starting in r2020b you can set the TitleHorizontalAlignment property of the axes to specify the justification (left|right|center).
There are similar properties for the xlabel and ylabel, too. See this Community Hightlight for a review.
https://www.mathworks.com/matlabcentral/discussions/highlights/133180
10 Comments Show 8 older commentsHide 8 older comments
Show 8 older commentsHide 8 older comments
dpb on 15 Feb 2021
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_1330497
Kewl. I'm on R2019b.
Didn't fix unalterable position for the SGTITLE() by any chance, did they?
dpb on 15 Feb 2021
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_1330587
Open in MATLAB Online
With this, above could be
...
for i=1:8
hAx(i)=subplot(4,2,i);
hT(i)=title(str{i});
hAx(i).TitleHorizontalAlignment=hposn{mod(i+1,2)+1};
end
...
perhaps a little cleaner.
Adam Danz on 15 Feb 2021
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_1330642
Open in MATLAB Online
It looks like sgtitle positions are still not editable.
Another alternative:
for i=1:8
hAx(i)=subplot(4,2,i);
title(str{i});
end
set(hAx(1:2:end),'TitleHorizontalAlignment', 'Left')
set(hAx(2:2:end),'TitleHorizontalAlignment', 'Right')
dpb on 15 Feb 2021
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_1330767
Edited: dpb on 15 Feb 2021
Yeah, good idea to fix up after the fact; requires set since dot notation won't accept arrays.
You can move sgtitle() around on the figure with the hidden properties so the on-screen version looks as modified; "SaveAs" causes the position to be overwritten, though. I didn't pursue any further to see if could manage to stop that with futzing with callbacks, etc., ...
evelyn on 6 Aug 2024 at 1:34
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_3230691
how to change 'sgtitle' from top to bottom?
Walter Roberson on 6 Aug 2024 at 2:42
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_3230731
@evelyn
sgtitle() returns a Text object. You can adjust the position of the Text object. It might take some playing with the coordinates, unless you set the Units property to 'normalized' and then use normalized coordinates.
evelyn on 6 Aug 2024 at 6:24
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_3230866
Open in MATLAB Online
thank you for your reply. How to change the position?
sgt = sgtitle('(a)')
sgt =
Text ((a)) with properties: String: '(a)' FontSize: 13 FontWeight: 'normal' FontName: 'Helvetica' Color: [0 0 0] Interpreter: 'tex' Use GET to show all properties
sgt.Position
Unrecognized method, property, or field 'Position' for class 'matlab.graphics.illustration.subplot.Text'.
Adam Danz on 6 Aug 2024 at 14:43
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_3231216
Open in MATLAB Online
@evelyn I suggest you use tiledlayout which supports global titles and labels.
You can use xlabel to assign a title to the bottom of the figure.
tcl = tiledlayout(2,2);
nexttile
title('a')
nexttile
title('b')
nexttile
title('c')
nexttile
title('d')
t = title(tcl,'Global title','FontSize', 20);
xlabel(tcl,'Global title on botton','FontSize', 20)
evelyn on 7 Aug 2024 at 5:12
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_3231724
thank you for your reply! I think that is what I need.
Is there any way to add title to the bottem of each subplot?
Adam Danz on 7 Aug 2024 at 12:23
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/745962-how-do-i-change-position-of-titles-and-subtitles-of-subplots#comment_3232014
You could use the xlabel function or text() but if the axes limits update, the relative text position may change.
Sign in to comment.
More Answers (1)
dpb on 15 Feb 2021
Open in MATLAB Online
Try
str=compose('Currents at %d mV ', [-70:10:0]);
hposn={'left','right'};
figure
for i=1:8
hAx(i)=subplot(4,2,i);
hT(i)=title(str{i},'Position',[mod(i+1,2) 1.03],'horizontalAlignment',hposn{mod(i+1,2)+1});
end
hSG=sgtitle('I is 2 times slower than E','fontsize',9);
hNC=hSG.NodeChildren.Children(2);
hNC.Position(2)=0.825;
Again, TMW got too clever for their own good; the 'Position' property of the text object that is the subplot grid title is hidden as is the text object itself buried deep inside the returned composite handle object. Consequently, moving it around a little is a pain.
On a default figure here, the y-position turns out to be 0.88; that appears to high for the effect I think you are looking for; the 0.825 is an empirical adjustment; also while the text is centered over the entire figure horizontally, it also appears somewhat off center owing to the placement of the axes inside the area; you can fiddle with positions of each of the subplots to close them up as desired and then adjust as needs be.
The above for just default produced:
ADDENDUM: Just realized that when saving the figure, the SGTITLE() text position gets overwritten back to default. That appears to be unavoidable; you might end up having to just use TEXT() directly instead, altho the locations of the titles are easy enough to handle.
You might want to investigate the tiledlayout instead, it may give you more nearly the spacings between plots you're looking for without as much mucking around as subplot requires. I've not messed with it much so can't say for sure...
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAxis Labels
Find more on Axis Labels in Help Center and File Exchange
Tags
- plot
- subplot
- plotting
- matlab
- title
- subtitle
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office