Add xline equivalent to stackedplot?
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Brock Carlson
le 28 Jan 2020
Modifié(e) : Adam Danz
le 20 Nov 2023
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/268860/TraditionalXlineWithSubplot.jpeg)
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/268861/stackedplot.jpeg)
and inserting a line on the matlab figure editor yields:
![stackedplot_withLine.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/268862/stackedplot_withLine.jpeg)
Thank you!
0 commentaires
Réponse acceptée
Adam Danz
le 19 Nov 2020
Modifié(e) : Adam Danz
le 20 Nov 2023
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 commentaires
Oscar
le 9 Août 2022
Great answer. Unfortunately, this method doesn't seem to work when the figure is embedded in a Live Script.
Plus de réponses (1)
Payas Bahade
le 14 Fév 2020
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 commentaires
Nicolas
le 5 Sep 2023
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!
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!