How to apply the same style/color and axes labels to all subplots?
32 views (last 30 days)
Show older comments
Hello, here is a snippet of code. The LineStyle, Color, Xlabel and Ylabel are the same for all four subplots. Is there a way to write this out once instead of copying and pasting those 4 lines under each subplot over and over. Thank you.
colors = ["k"; "r";"g";"r";"g"];
lines = ["-"; "-";"-";"--";"--"];
tiledlayout(2,2);
nexttile
hold on
for xx = 1:n
h = cdfplot(sort_er1(start(xx):end(xx),columnb));
h.LineStyle = lines(xx); %This is the same for all subplots
h.Color = colors(xx); %This is the same for all subplots
end
hold off
xlabel('B [%]') %This is the same for all subplots
ylabel('Probability') %This is the same for all subplots
title('er1')
nexttile
hold on
for xx = 1:n
h = cdfplot(sort_er2(start(xx):end(xx),columnb));
end
hold off
title('er2')
nexttile
hold on
for xx = 1:n
h = cdfplot(sort_er3(start(xx):end(xx),columnb));
end
hold off
title('er3')
nexttile
hold on
for xx = 1:n
h = cdfplot(sort_er4(start(xx):end(xx),columnb));
end
hold off
title('er4')
0 Comments
Accepted Answer
KSSV
on 28 Feb 2023
colors = ["k"; "r";"g";"r";"g"];
lines = ["-"; "-";"-";"--";"--"];
tiledlayout(2,2);
ax(1) = nexttile ;
plot(rand(10,1)) ;
title('er1')
ax(2) = nexttile ;
plot(rand(10,1)) ;
title('er2')
ax(3) = nexttile ;
plot(rand(10,1)) ;
title('er3')
ax(4) = nexttile ;
plot(rand(10,1)) ;
title('er4')
xlabel(ax,'B [%]') %This is the same for all subplots
ylabel(ax,'Probability') %This is the same for all subplots
More Answers (1)
Simon Chan
on 28 Feb 2023
Another approach for your consideration.
Noticed that the following example is only applicable provided all subplots have the same number of lines.
A = randi(100,10,5,4);
t = tiledlayout(2,2);
nexttile
plot(A(:,:,1));
nexttile
plot(A(:,:,2));
nexttile
plot(A(:,:,3));
nexttile
plot(A(:,:,4));
objAx = findobj(t.Children,'Type','Axes'); % Find objects with Type Axes
ylabel(objAx,'Same YLabel'); % Write YLabel
xlabel(objAx,'Same XLabel'); % Write XLabel
%
colors = ["k";"r";"g";"r";"g"];
colorsAll = repmat(colors,length(objAx),1); % Provided all subplots have the same number of lines
lines = ["-";"-";"-";"--";"--"];
linesAll = repmat(lines,length(objAx),1); % Provided all subplots have the same number of lines
obj = findobj(t.Children,'Type','Line'); % Find objects with Type Line
for k = 1:length(obj)
obj(k).Color = colorsAll(k); % Change line color
obj(k).LineStyle = linesAll(k); % Change line style
end
0 Comments
See Also
Categories
Find more on Subplots 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!