How do I change position of titles and subtitles of subplots?

276 views (last 30 days)
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!

Accepted Answer

Adam Danz
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.
  4 Comments
Adam Danz
Adam Danz on 15 Feb 2021
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
dpb on 15 Feb 2021
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., ...

Sign in to comment.

More Answers (1)

dpb
dpb on 15 Feb 2021
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...

Categories

Find more on Line 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!