Hi there,
I recently started using stackedplot instead of subplot, but I'm not sure how to add a line from 0 to my Y-axis-limit while using the stackedplot funciton.
While using subplot in the past, I have been able to use xline(0) or vline(0) to represent a stimulus onset time when comparing evoked responses across a certain timecourse. I.e.
%%%%% Using xline in subplots
close all
time = -150:1:349; %This is time in ms relative to stimulus onset at time 0
maxYval = max(max(PlotThis));
figure
for i = 1:3
subplot(3,1,i)
plot(time,PlotThis(:,i));
hold on
xline(0)
ylim([0 maxYval])
end
TraditionalXlineWithSubplot.jpg
I would like to use stackedplots instead of subplot because it is much easier to edit and manage when I have several plots with the same x-axis. The one drawback is that stackedplots does not allow the "hold on" feature so xline(0) or vline(0) does not work. I can insert a line in-post on the matlab figure editor, but is there a way to do this programitically?
i.e.
%%% using stackedplot
figure
s = stackedplot(time,PlotThis);
s.AxesProperties(1).YLimits = [0 maxYVal];
s.AxesProperties(2).YLimits = [0 maxYVal];
s.AxesProperties(3).YLimits = [0 maxYVal];
stackedplot.jpg
and inserting a line on the matlab figure editor yields:
stackedplot_withLine.jpg
Thank you!

 채택된 답변

Adam Danz
Adam Danz 2020년 11월 19일
편집: Adam Danz 2023년 11월 20일

2 개 추천

You can get the axis handles in stackedplot using the undocumented NodeChildren property. Then use xline to add vertical reference lines.
rng('default')
h = stackedplot(-10:10,rand(21,4));
ax = findobj(h.NodeChildren, 'Type','Axes');
arrayfun(@(h)xline(h,0,'LineWidth',1.5),ax)
% Starting R2021a, xline and yline can accept an array of handles
% xline(ax,0,'LineWidth',1.5)

댓글 수: 7

Brock Carlson
Brock Carlson 2020년 11월 20일
Adam,
Thank you so much! This is a fascinating work-around. I was able to get this to work with my code.
As a result, I have unaccepted Payas' answer and accepted yours. Should MathWorks create a formal solution in the future I will try to update that here.
Adam Danz
Adam Danz 2020년 11월 24일
Glad I could help.
I just updated the answer with a better way to get axis handles.
Is there a way to add labels to these lines? I want to add a label, such as Ns, Ea, Md, etc. to the lines (I have the array of labels below and I attached the snap of the figure - the figure itself was too large to attach, even in a zip file).
['Ns','08','15','22','Ns','Ec','Mo','Ns','08','15','22','Ns','Mo','Mt','Ea','Md','Hr','Mt','Mt','Ea','Md','Hr']
Adam Danz
Adam Danz 2021년 10월 31일
편집: Adam Danz 2021년 10월 31일
Yes, the 3rd input to xline is the label in the form of a string or character array.
help xline
Thank you!
This is a similar bit of code I used for the same purpose:
sp=stackedplot(T,T.Properties.VariableNames(2:end),'XVariable','Timestamp')
ax = findobj(sp.NodeChildren, 'Type','Axes');
arrayfun(@(sp)xline(sp,EvtMar,'--',{'EO','BE','BE','BE','EC','BE','CE','CE','BE','OM','OM'}),ax);
grid on; xlabel('Timestamp (s)'); %turns on grid and labels the x-axis
title('EEG Signals - Stacked Plot');
fig0.WindowState = 'maximize'; %maximizes the figure window -> easier to see
Adam Danz
Adam Danz 2021년 11월 3일
편집: Adam Danz 2021년 11월 3일
I don't know when this option became available but in Matlab R2021b you can enter mutliple values for xline and yline.
xline(1:5,'k-', {'a' 'b' 'c' 'd' 'e'})
xlim([1,5.5])
Oscar
Oscar 2022년 8월 9일
Great answer. Unfortunately, this method doesn't seem to work when the figure is embedded in a Live Script.

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

추가 답변 (1개)

Payas Bahade
Payas Bahade 2020년 2월 14일

0 개 추천

Hi Brock,
Currently there is no xline equivalent in stacked plots. I have informed the concerned teams and they would be looking into this issue.

댓글 수: 2

Brock Carlson
Brock Carlson 2020년 2월 17일
Thank you very much! I'll be excited to see updates if they become available.
Nicolas
Nicolas 2023년 9월 5일
Hello Payas,
Any update on this? Stacked plots are really neat to quickly draw stuff, adding the ability to add annotations such as vline would be a real plus!

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

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2020년 1월 28일

편집:

2023년 11월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by