embedding inset plots in subplots matlab

14 views (last 30 days)
Hi, I am trying to plots an insets plots in aubplots but only the last subplot and inset plots are shown. Please can someone help with this?
load my_data
% Define xi_select and slope_select for main and inset plots
xi_select = [1, 10, 11, 12];
slope_select = [1, 3, 4, 5, 7, 9];
% Create a tiled layout with 2 rows and 3 columns of subplots
t = tiledlayout(2, 3, 'TileSpacing', 'compact', 'Padding', 'compact');
% Define custom colors (green to red gradient)
num_colors = length(xi_select);
custom_colors = [linspace(0, 1, num_colors)', linspace(1, 0, num_colors)', zeros(num_colors, 1)];
custom_linestyles = repmat({'--'}, 1, num_colors); % All dashed lines
% Initialize arrays for legend
legend_labels = {}; % Cell array to store legend labels
legend_index = 0; % Initialize legend index
plot_handles = []; % Array for storing plot handles
for j = 1:length(slope_select) % Loop through selected ramp_slope
% Create the main subplot
ax = nexttile; % Move to the next tile for each subplot
hold(ax, 'on'); % Hold on to plot multiple lines in the same subplot
% Loop through selected xi values for the main plots
for i = 1:length(xi_select)
% Find the switching times for the current xi and ramp_slope
transitions = find(switching(:, xi_select(i), slope_select(j)));
if ~isempty(transitions)
% Plot the uxlt data at the switching times for this xi and ramp_slope
for t_idx = transitions
% Extract the uxlt data for the current time point
uxlt_data = squeeze(uxlt(ym, :, xi_select(i), slope_select(j))); % Dimension: y, xi, ramp_slope
% Proper LaTeX syntax for the legend entry, omitting time
display_name = sprintf('$\\xi=%.2f$', xi(xi_select(i)));
% Plot the data with specified color and linestyle
h = plot(ax, tarray, uxlt_data, 'LineWidth', 1.5, ...
'Color', custom_colors(i, :), 'LineStyle', custom_linestyles{i});
% Only add the label and handle once to the legend
if ~ismember(display_name, legend_labels)
legend_index = legend_index + 1;
plot_handles(legend_index) = h(1); % Store only the first handle
legend_labels{legend_index} = display_name; % Append label
end
end
end
end
% Add labels and title to each subplot
xlabel(ax, 'Time (s)', 'Interpreter', 'latex');
ylabel(ax, '$\int u_L \, \mathrm{dz}$', 'Interpreter', 'latex');
xlim(ax, [min(tarray), max(tarray)]);
ylim(ax, [0, 2.5e-04]);
% Add subplot numbering and S_{rf} value
title(ax, sprintf('(%c) $S_{rf} = %.2f$', 'a' + j - 1, ramp_slope(slope_select(j))), 'Interpreter', 'latex');
box(ax, 'on'); % Place a box around the current subplot
% Add inset plots
inset_pos = ax.Position; % Get the position of the current subplot
if j == 1
% The first inset plot on the top left of the first subplot
inset_pos = [inset_pos(1) + 0.1 * inset_pos(3), inset_pos(2) + 0.65 * inset_pos(4), 0.15, 0.15];
else
% Remaining inset plots on the top right of the respective subplots
inset_pos = [inset_pos(1) + 0.6 * inset_pos(3), inset_pos(2) + 0.65 * inset_pos(4), 0.15, 0.15];
end
% Create new axes for inset plots, without attaching them to the main subplot axes
inset_ax = axes('Position', inset_pos); % Create inset axes with adjusted position
hold(inset_ax, 'on'); % Hold on inset axes to plot multiple lines
% Loop through selected xi values for the inset plots
for i = 1:length(xi_select)
% Find the switching times for the current xi and ramp_slope
transitions = find(switching(:, xi_select(i), slope_select(j)));
if ~isempty(transitions)
% Plot the uxl data in the inset plot
for t_idx = transitions
% Extract the uxl data for the current time point
uxl_data = squeeze(uxl(:, xi_select(i), slope_select(j))); % Dimension: y, xi, ramp_slope
% Plot the data in the inset with color and linestyle
plot(inset_ax, y, uxl_data, 'LineWidth', 1.5, ...
'Color', custom_colors(i, :), 'LineStyle', custom_linestyles{i});
end
end
end
% Add labels to inset
xlabel(inset_ax, '$z$', 'Interpreter', 'latex');
ylabel(inset_ax, '$\int u_L \, \mathrm{dz}$', 'Interpreter', 'latex');
xlim(inset_ax, [min(y), max(y)]);
hold(inset_ax, 'off'); % Release hold on the inset axes
end
Unrecognized function or variable 'ym'.
% Create a global legend (optional, if needed)
lgd = legend(plot_handles, legend_labels, 'Interpreter', 'latex', 'NumColumns', 2);
% Ensure that the entire figure has a box around it
box on;
  4 Comments
Voss
Voss on 28 Sep 2024
Thanks. Looks like ym is still missing (and possibly other variables).

Sign in to comment.

Accepted Answer

Voss
Voss on 28 Sep 2024
Supplying the tiledlayout t to nexttile (i.e., specifying nexttile(t)) seems to fix the problem.
load my_data
ym = 16;
% Define xi_select and slope_select for main and inset plots
xi_select = [1, 10, 11, 12];
slope_select = [1, 3, 4, 5, 7, 9];
% Create a tiled layout with 2 rows and 3 columns of subplots
t = tiledlayout(2, 3, 'TileSpacing', 'compact', 'Padding', 'compact');
% Define custom colors (green to red gradient)
num_colors = length(xi_select);
custom_colors = [linspace(0, 1, num_colors)', linspace(1, 0, num_colors)', zeros(num_colors, 1)];
custom_linestyles = repmat({'--'}, 1, num_colors); % All dashed lines
% Initialize arrays for legend
legend_labels = {}; % Cell array to store legend labels
legend_index = 0; % Initialize legend index
plot_handles = []; % Array for storing plot handles
for j = 1:length(slope_select) % Loop through selected ramp_slope
% Create the main subplot
ax = nexttile(t); % Move to the next tile for each subplot
hold(ax, 'on'); % Hold on to plot multiple lines in the same subplot
% Loop through selected xi values for the main plots
for i = 1:length(xi_select)
% Find the switching times for the current xi and ramp_slope
transitions = find(switching(:, xi_select(i), slope_select(j)));
if ~isempty(transitions)
% Plot the uxlt data at the switching times for this xi and ramp_slope
for t_idx = transitions
% Extract the uxlt data for the current time point
uxlt_data = squeeze(uxlt(ym, :, xi_select(i), slope_select(j))); % Dimension: y, xi, ramp_slope
% Proper LaTeX syntax for the legend entry, omitting time
display_name = sprintf('$\\xi=%.2f$', xi(xi_select(i)));
% Plot the data with specified color and linestyle
h = plot(ax, tarray, uxlt_data, 'LineWidth', 1.5, ...
'Color', custom_colors(i, :), 'LineStyle', custom_linestyles{i});
% Only add the label and handle once to the legend
if ~ismember(display_name, legend_labels)
legend_index = legend_index + 1;
plot_handles(legend_index) = h(1); % Store only the first handle
legend_labels{legend_index} = display_name; % Append label
end
end
end
end
% Add labels and title to each subplot
xlabel(ax, 'Time (s)', 'Interpreter', 'latex');
ylabel(ax, '$\int u_L \, \mathrm{dz}$', 'Interpreter', 'latex');
xlim(ax, [min(tarray), max(tarray)]);
ylim(ax, [0, 2.5e-04]);
% Add subplot numbering and S_{rf} value
title(ax, sprintf('(%c) $S_{rf} = %.2f$', 'a' + j - 1, ramp_slope(slope_select(j))), 'Interpreter', 'latex');
box(ax, 'on'); % Place a box around the current subplot
% Add inset plots
inset_pos = ax.Position; % Get the position of the current subplot
if j == 1
% The first inset plot on the top left of the first subplot
inset_pos = [inset_pos(1) + 0.1 * inset_pos(3), inset_pos(2) + 0.65 * inset_pos(4), 0.15, 0.15];
else
% Remaining inset plots on the top right of the respective subplots
inset_pos = [inset_pos(1) + 0.6 * inset_pos(3), inset_pos(2) + 0.65 * inset_pos(4), 0.15, 0.15];
end
% Create new axes for inset plots, without attaching them to the main subplot axes
inset_ax = axes('Position', inset_pos); % Create inset axes with adjusted position
hold(inset_ax, 'on'); % Hold on inset axes to plot multiple lines
% Loop through selected xi values for the inset plots
for i = 1:length(xi_select)
% Find the switching times for the current xi and ramp_slope
transitions = find(switching(:, xi_select(i), slope_select(j)));
if ~isempty(transitions)
% Plot the uxl data in the inset plot
for t_idx = transitions
% Extract the uxl data for the current time point
uxl_data = squeeze(uxl(:, xi_select(i), slope_select(j))); % Dimension: y, xi, ramp_slope
% Plot the data in the inset with color and linestyle
plot(inset_ax, y, uxl_data, 'LineWidth', 1.5, ...
'Color', custom_colors(i, :), 'LineStyle', custom_linestyles{i});
end
end
end
% Add labels to inset
xlabel(inset_ax, '$z$', 'Interpreter', 'latex');
ylabel(inset_ax, '$\int u_L \, \mathrm{dz}$', 'Interpreter', 'latex');
xlim(inset_ax, [min(y), max(y)]);
hold(inset_ax, 'off'); % Release hold on the inset axes
end
% Create a global legend (optional, if needed)
lgd = legend(plot_handles, legend_labels, 'Interpreter', 'latex', 'NumColumns', 2);
% Ensure that the entire figure has a box around it
box on;

More Answers (1)

Image Analyst
Image Analyst on 29 Sep 2024
For what it's worth (may help someone in the future) I'm attaching my demos for inserting inset plots and images inside other plots and images.

Categories

Find more on Data Exploration in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!