How to apply the same style/color and axes labels to all subplots?
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')
Accepted Answer
More Answers (1)
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
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!