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

조회 수: 315 (최근 30일)
Samuele Bolotta
Samuele Bolotta 2021년 2월 15일
편집: dpb 2021년 2월 15일
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!

채택된 답변

Adam Danz
Adam Danz 2021년 2월 15일
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
Adam Danz
Adam Danz 2021년 2월 15일
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 2021년 2월 15일
편집: dpb 2021년 2월 15일
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., ...

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

추가 답변 (1개)

dpb
dpb 2021년 2월 15일
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...

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by