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

조회 수: 33 (최근 30일)
Macy
Macy 2023년 2월 28일
답변: Simon Chan 2023년 2월 28일
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')

채택된 답변

KSSV
KSSV 2023년 2월 28일
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
Macy
Macy 2023년 2월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
Simon Chan 2023년 2월 28일
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

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by