embedding inset plots in subplots matlab

14 visualizaciones (últimos 30 días)
University
University el 28 de Sept. de 2024
Respondida: Image Analyst el 29 de Sept. de 2024
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 comentarios
Voss
Voss el 28 de Sept. de 2024
Thanks. Looks like ym is still missing (and possibly other variables).
University
University el 28 de Sept. de 2024
sorry, ym=16

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 28 de Sept. de 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;
  2 comentarios
University
University el 28 de Sept. de 2024
Thank you for your help.
Voss
Voss el 28 de Sept. de 2024
You're welcome!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 29 de Sept. de 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.

Categorías

Más información sobre Data Exploration en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by