How adding second grid and values in stackedplot
11 views (last 30 days)
Show older comments
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/963345/image.png)
0 Comments
Answers (2)
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);
0 Comments
Seth Furman
on 23 Sep 2022
Plotting horizontal lines with stackedplot is now easier with support for multiple table inputs.
Load data
tbl = array2table(magic(3))
Create table defining horizontal lines
lines = array2table(repmat([5;5],1,width(tbl)))
Define common x-values
tbl.x = (1:height(tbl))'
lines.x = [1;height(tbl)]
Plot data with horizontal lines
sp = stackedplot(tbl,lines,XVariable="x",XLabel="Row");
Customize chart appearance
sp.GridVisible = "on";
sp.LegendVisible = "off";
for i = 1:numel(sp.LineProperties)
sp.LineProperties(i).Color(2,:) = [0 0 0];
sp.LineProperties(i).LineStyle = ["-","--"];
sp.AxesProperties(i).LegendVisible = "off";
end
0 Comments
See Also
Categories
Find more on Surface and Mesh 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!