How adding second grid and values in stackedplot

5 views (last 30 days)
Rachele Franceschini
Rachele Franceschini on 13 Apr 2022
Answered: Simon Chan on 13 Apr 2022
I have this code for creating stackedplot. But I would like that within plot there were other line in y. As in the figure, I would like value along y-axis, but also the secondary line (in black). How can I do?
tbl = readtable("Cartel5.xlsx","TextType","string");
head(tbl,3)
tbl=tbl(:,1:9)
tbl1 = readtable("Cartel7.xlsx","TextType","string");
head(tbl1,3)
tbl1=tbl1(:,1:9)
tbl2 = readtable("Copy_of_Cartel7.xlsx","TextType","string");
head(tbl2,3)
tbl2=tbl2(:,1:9)
new_table = [tbl, tbl1, tbl2];
vars = {["A","ABR","ab"],["B","BAS","ba"],["C","CAL","cl"],["D","CAM","cm"],["E","EMR","em"],["F","FVG","fr"],["G","LAZ","la"],["H","LIG","li"]}
s=stackedplot(new_table,vars,"LineWidth",2,"FontSize",12,"GridVisible","on")
s.AxesProperties(1).LegendVisible = 'off'
s.AxesProperties(2).LegendVisible = 'off'
s.AxesProperties(3).LegendVisible = 'off'
s.AxesProperties(4).LegendVisible = 'off'
s.AxesProperties(5).LegendVisible = 'off'
s.AxesProperties(6).LegendVisible = 'off'
s.AxesProperties(7).LegendVisible = 'off'
s.AxesProperties(8).LegendVisible = 'off'
s.AxesProperties(1).YLimits = [0 25];
s.AxesProperties(2).YLimits = [0 15];
s.AxesProperties(3).YLimits = [0 5.50];
s.AxesProperties(4).YLimits = [0 5.50];
s.AxesProperties(5).YLimits = [0 5.50];
s.AxesProperties(6).YLimits = [0 5.50];
s.AxesProperties(7).YLimits = [0 5.50];
s.AxesProperties(8).YLimits = [0 5.50];
ax = findobj(s.NodeChildren, 'Type','Axes');
arrayfun(@(s)yline(s,5,'LineWidth',1.5),ax)

Answers (1)

Simon Chan
Simon Chan on 13 Apr 2022
Set the YLim and YTick for that particular axis.
X = 0:4:20;
Y = randi(5,6,3);
f = figure;
s = stackedplot(f,X,Y);
s.GridVisible='on';
ax = findobj(s.NodeChildren, 'Type','Axes');
ax(2).YLim = [-1 6]; % For axis #2 only for demonstration
ax(2).YTick=[0 2 5];
ax(2).YTickLabel = arrayfun(@(x) sprintf('%d',x),ax(2).YTick,'uni',0);

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by