How to label x-axis for multiple subplots with different names?
17 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 16 Jul 2017
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 Comments
More Answers (1)
Walter Roberson
on 16 Jul 2017
Edited: Walter Roberson
on 16 Jul 2017
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
See Also
Categories
Find more on Axis Labels 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!