How adding second grid and values in stackedplot

18 次查看(过去 30 天)
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)

回答(2 个)

Simon Chan
Simon Chan 2022-4-13
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);

Seth Furman
Seth Furman 2022-9-23
Plotting horizontal lines with stackedplot is now easier with support for multiple table inputs.

Load data

tbl = array2table(magic(3))
tbl = 3×3 table
Var1 Var2 Var3 ____ ____ ____ 8 1 6 3 5 7 4 9 2

Create table defining horizontal lines

lines = array2table(repmat([5;5],1,width(tbl)))
lines = 2×3 table
Var1 Var2 Var3 ____ ____ ____ 5 5 5 5 5 5

Define common x-values

tbl.x = (1:height(tbl))'
tbl = 3×4 table
Var1 Var2 Var3 x ____ ____ ____ _ 8 1 6 1 3 5 7 2 4 9 2 3
lines.x = [1;height(tbl)]
lines = 2×4 table
Var1 Var2 Var3 x ____ ____ ____ _ 5 5 5 1 5 5 5 3

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

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by