Plotting subplots in one figure, with shared axes and labels
32 views (last 30 days)
Show older comments
This segment of code is supposed to plot some plots in (9 subplots for each figure) arrangment. However, I am trying to make a shared y-axis for each row and a shared x-axis for each column and keep only one axis label on each axis. This code works perfectly fine for the first figure that is plotted, however, for the next figures it does not apply the same formatting on them.If anyone can help in resolving this.
the figure "i1" shows what I am getting for the first figure and what I want, and the figure "i2" shows what I am getting for the other figures. we want to keep the formatting of all figers as figure one.
attached is a sample of the data being plotted.
This is the segment of code used:
% ii represents the number of plots that we have to plot. In this case ii=86
numFigures = ceil(ii / 9);
% Check if a new figure is needed
if mod(ii - 1, 9) == 0
% Create a new figure
hFig = figure('Name', ['Combined Plots Figure ', num2str(numFigures)], 'NumberTitle', 'off');
numFigures = numFigures + 1;
end
% Plot each set of 9 plots together
subplot(3, 3, mod(ii - 1, 9) + 1, 'Parent', hFig);
hold on;
% Plot data
plot(xss, updatedy, '-x', 'color', "#000000", 'LineWidth', 1)
% Adjust axes and labels for all figures
if mod(ii - 1, 3) == 0
ylabel('Height (km)');
else
set(gca, 'YTickLabel', []);
end
if ii > 6
xlabel('Ne (el/m^3)');
else
set(gca, 'XTickLabel', []);
end
ylim([100 800])
grid on
hold off
3 Comments
Dyuman Joshi
on 25 Dec 2023
@Salma fathi, I see that you have updated the question, however you have not answered my questions.
Are you running a for loop?
What is ii? If it is 86, then is the output supposed to be 9 figures with 9 subplots and 1 figure with 5 subplots?
Accepted Answer
Dyuman Joshi
on 25 Dec 2023
This is a general idea, you can make changes to it as you like.
load('data.mat')
ii=86;
for k=1:floor(ii/9)
hFig = figure(k);
t = tiledlayout(3, 3, 'Parent', hFig);
for n=1:9
nexttile
plot(xss, updatedy, '-x', 'color', "#000000", 'LineWidth', 1)
if ~ismember(n, [1 4 7])
yticks([]);
end
if ~ismember(n, [7 8 9])
xticks([])
end
end
title(t, sprintf('Figure %d', k))
ylabel(t, 'Height (km)');
xlabel(t, 'Ne (el/m^3)');
end
2 Comments
More Answers (0)
See Also
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!