How to label x-axis for multiple subplots with different names?

조회 수: 26 (최근 30일)
Penny Chen
Penny Chen 2017년 7월 16일
댓글: Star Strider 2021년 6월 13일
I have plotted multiple plots in a single figure in matlab. Now I want to label axes (X, Y) labels with different name (ex: A1, A2). How can I do that?
I have tried with the following codes, however the problem is that I don't know how to assign different names in the for loop.
for i = 1:1:2
subplot(1,2,i)
plot(t,X(:,i))
xlabel('time');
ylabel('here I want to put different names, for the first subplot, I want it to be A1, for the second subplot, I want it to be A2,');
hold on
end

채택된 답변

Star Strider
Star Strider 2017년 7월 16일
Create a cell array with the different y-axis labels, then index into it:
y_label_names = {'Subplot 1', 'Subplot 2', 'Subplot 3', 'Subplot 4', 'Subplot 5', 'Subplot 6', 'Subplot 7', 'Subplot 8', 'Subplot 9'};
t = 1:20; % Create Data
X = rand(20,9); % Create Data
for i = 1:1:9
subplot(2,5,i)
plot(t,X(:,i))
xlabel('time');
ylabel(y_label_names{i});
hold on
end
Change the nine strings in ‘y_axis_names’ to the ones you want.
  댓글 수: 4
Indah Kartika
Indah Kartika 2021년 6월 13일
so helpful. Thank you!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 7월 16일
편집: Walter Roberson 2017년 7월 16일
names = {'John', 'Paul', 'George', 'Ringo', 'Marie', 'Lise', 'Ada', 'Hypatia', 'Heddy'};
for i = 1:1:9
subplot(2,5,i)
plot(t,X(:,i))
xlabel('time');
ylabel( sprintf('%s is #%d', names{i}, 10-i) );
hold on
end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by