How to apply the same style/color and axes labels to all subplots?

31 vues (au cours des 30 derniers jours)
Macy
Macy le 28 Fév 2023
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')

Réponse acceptée

KSSV
KSSV le 28 Fév 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
  1 commentaire
Macy
Macy le 28 Fév 2023
Thank you, I was able to apply the axes labels. Do you know about the LineStyle and line Colors, however? I am not sure how to apply those once.

Connectez-vous pour commenter.

Plus de réponses (1)

Simon Chan
Simon Chan le 28 Fév 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

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by