Plotting subplots in one figure, with shared axes and labels

32 visualizzazioni (ultimi 30 giorni)
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 Commenti
Dyuman Joshi
Dyuman Joshi il 25 Dic 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?
Salma fathi
Salma fathi il 25 Dic 2023
Modificato: Salma fathi il 25 Dic 2023
Thank you for your comment.
Yes I am using a for loop to plot the output, each iteration will result in a subplot. ii is the number of iterations that we have, i.e. the number of subplots that we have. And you are correct the output would be 9 figures with 9 subplots and 1 figure with 5 subplots.
I am trying to have a shared y-axis for each row of subplots in a figure, and a shared x-axis for each column of subplots.

Accedi per commentare.

Risposta accettata

Dyuman Joshi
Dyuman Joshi il 25 Dic 2023
A better option would be to use tiledlayout here as you can make shared axis labels and titles -
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

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by