How to create figure containing 4 plots with tiledlayout for different values of a parameter?

1 view (last 30 days)
Hello to everyone,
I'm dealing with a post-processing of large astrodynamics trajectories database and I need to put together 4 figures in one by using tiledlayout matlab function. I want to get 4 plot (one for each value contained inside the array "idx_JC_vec") in one figure and then export it as .pdf file.
Here is my code (sorry for its):
load UHIM_DB % I reduce the original database to only 4 values of Jacobi constant
load Customized_colors
%Trajectories family selection
% idx_L1 = 1
% idx_L2 = 2
% idx_JC = 16 % From 1 to 50
idx_JC_vec = [16,26,36,46]; % I WANT TO SCROLL THIS ARRAY TO GET THE FIGURE WITH 4 PLOTS
t=tiledlayout(2,2,"Padding","compact","TileSpacing","compact");
for k=1:numel(idx_JC_vec)
nexttile
idx_JC = idx_JC_vec(k);
% Colos setting
colors_HIM = [1 0.3 0.3;0.3 0.8 0.9];
colors_PLO = [my_red;my_blue];
%fig = figure;
hold on;axis equal; grid on; box on;
UHIM_plots = zeros(1,2); % preallocation
text_pos =[-0.0012,-0.0025;
-0.00075,-0.0025];
% Plot trajectories of unstable manifold
for idx_L=1:2
lgd_label = ['$JC =$',num2str(UHIM_DB(idx_L).data(idx_JC).JC,'%9.6f')];
UHIM_plots(idx_L) = plot([UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.x],...
[UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.y],'Color',colors_HIM(idx_L,:),'LineWidth',0.3,'DisplayName',lgd_label);
% Plot PLOs
plot([UHIM_DB(idx_L).data(idx_JC).PLO.x],[UHIM_DB(idx_L).data(idx_JC).PLO.y],'Color',colors_PLO(idx_L,:),'LineWidth',0.8);
% Plot L-points
plot(UHIM_DB(idx_L).L_point_pos.x,UHIM_DB(idx_L).L_point_pos.y,'.',...
'color',[0,0,0],'MarkerFaceColor',my_green,'MarkerSize',10);
text(UHIM_DB(idx_L).L_point_pos.x+text_pos(idx_L,1),UHIM_DB(idx_L).L_point_pos.y+text_pos(idx_L,2),['$L_{',num2str(idx_L),'}$'],'Interpreter',"latex");
end
% Selection of CI radius based on Jcbi cst value (Ly orbit size)
if (idx_JC >=1 && idx_JC <=5)
factor_k = 2.0
elseif (idx_JC >=6 && idx_JC <=15)
factor_k = 2.5
elseif (idx_JC >=16 && idx_JC <=25)
factor_k = 3.0
elseif (idx_JC >=26 && idx_JC <=40)
factor_k = 3.5
elseif (idx_JC >=41 && idx_JC <=50)
factor_k = 4.0
end
rCI_EM_km = factor_k .* 9.29e+05;
rCI_EM = rCI_EM_km / 1.496e+08;
mu = 3.04042342644015e-06;
% Circles data
xC = (mu-1);
yC = 0;
angle = 0:0.1:360;
circX = xC + rCI_EM*cosd(angle); circY = yC + rCI_EM*sind(angle);
% legend labels
k_value = ['$k =\ $',num2str(factor_k)];
r_CI_value = ['$r_{CI} =\ $',num2str(rCI_EM_km,'%3.2e'),'$\ km$'];
% Plot CI
CI_plot=plot(circX, circY,'Color',[0 1 0],'LineWidth',0.7,'DisplayName',r_CI_value);
% Plot Earth + Moon system
plot(mu -1,0,'.',...
'color',my_blue,'MarkerFaceColor',my_blue,'MarkerSize',20);
text(mu-1.0,0-0.0025,'$E/M$','Interpreter',"latex",'Color',my_blue);
%Legend
lgd = legend([UHIM_plots,CI_plot],'Orientation',"vertical",'Location',"northeast");
lgd.Interpreter = 'latex';
lgd.FontSize = 9;
% dim = [.24 .835 .150 .06];
%
% str = ['$JC\ index = $ ',num2str(idx_JC),newline,...
% '$No.\ traj.\ L_1 = $',num2str(numel([UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.x])/200),newline,...
% '$No.\ traj.\ L_2 = $',num2str(numel([UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.x])/200)];
% annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','on',...
% 'BackgroundColor',[1 1 1],'FontSize',8);
% Axes lables
xlabel(t,'$x$ (au)','interpreter','latex','fontsize',12); ylabel(t,'$y$ (au)','interpreter','latex','FontSize',12);
% Option to center better the curves inside the plot window
x_lim = xlim; y_lim = ylim;
xlim([x_lim(1)-0.0*norm(x_lim) x_lim(2)+0.0*norm(x_lim)]);
ylim([y_lim(1)-0.1*norm(y_lim) y_lim(2)+0.4*norm(y_lim)]);
title(k_value,'Interpreter','latex');
end
factor_k = 3
factor_k = 3.5000
factor_k = 3.5000
factor_k = 4
%exportgraphics(fig,['fig_UHIM_traj_CI_JC_id_',num2str(idx_JC),'.pdf'],'ContentType','vector')
%%save as pdf
I want to get a figure with 4 plots like the one above according this layout (taken from tiledlayout documentation page):
EDIT(08/10/2022, 21:46): How can I get all equal tiles and with largest size inside my figure?

Accepted Answer

Star Strider
Star Strider on 8 Oct 2022
Edited: Star Strider on 8 Oct 2022
The essence of creating the tiledlayout here is:
tiledlayout(2,2)
for k = 1:numel(idx_JC_vec)
nexttile
idx_JC = idx_JC_vec(k)
. . . CODE . . .
end
I believe this is close to what you want to do.
There is an indexing error in the original code that I cannot locate, so I leave that to you. When that problem is solved, the rest should work and create the desired result. (That problem existed before I added the loop, so unless there is another problem, the loop is likely not the source of it.)
I usually use ‘k’ (or some version of it, such as ‘k1’, ‘k2’, etc.) as the loop index. I did not see any other instances of it, however if it causes problems, change it since it only appears in my loop code in the first three lines.
EDIT — (8 Oct 2022 at 17:46)
EDIT — (8 Oct 2022 at 18:16)
I thought both ‘UHIM_DB.mat’ files were the same. They aren’t. The second one has the missing (or corrected) variables.
% load UHIM_DB % I reduce the original database to only 4 values of Jacobi constant
% load Customized_colors
load(websave('UHIM_DB','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1149585/UHIM_DB.mat'))
load(websave('Customized_colors','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1149570/Customized_colors.mat'))
% load UHIM_DB % I reduce the original database to only 4 values of Jacobi constant
% load Customized_colors
%Trajectories family selection
% idx_L1 = 1
% idx_L2 = 2
% idx_JC = 16 % From 1 to 50
idx_JC_vec = [16,26,36,46]; % I WANT TO SCROLL THIS ARRAY TO GET THE FIGURE WITH 4 PLOTS
t=tiledlayout(2,2,"Padding","compact","TileSpacing","compact");
for k=1:numel(idx_JC_vec)
nexttile
idx_JC = idx_JC_vec(k);
% Colos setting
colors_HIM = [1 0.3 0.3;0.3 0.8 0.9];
colors_PLO = [my_red;my_blue];
%fig = figure;
hold on;axis equal; grid on; box on;
UHIM_plots = zeros(1,2); % preallocation
text_pos =[-0.0012,-0.0025;
-0.00075,-0.0025];
% Plot trajectories of unstable manifold
for idx_L=1:2
lgd_label = ['$JC =$',num2str(UHIM_DB(idx_L).data(idx_JC).JC,'%9.6f')];
UHIM_plots(idx_L) = plot([UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.x],...
[UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.y],'Color',colors_HIM(idx_L,:),'LineWidth',0.3,'DisplayName',lgd_label);
% Plot PLOs
plot([UHIM_DB(idx_L).data(idx_JC).PLO.x],[UHIM_DB(idx_L).data(idx_JC).PLO.y],'Color',colors_PLO(idx_L,:),'LineWidth',0.8);
% Plot L-points
plot(UHIM_DB(idx_L).L_point_pos.x,UHIM_DB(idx_L).L_point_pos.y,'.',...
'color',[0,0,0],'MarkerFaceColor',my_green,'MarkerSize',10);
text(UHIM_DB(idx_L).L_point_pos.x+text_pos(idx_L,1),UHIM_DB(idx_L).L_point_pos.y+text_pos(idx_L,2),['$L_{',num2str(idx_L),'}$'],'Interpreter',"latex");
end
% Selection of CI radius based on Jcbi cst value (Ly orbit size)
if (idx_JC >=1 && idx_JC <=5)
factor_k = 2.0
elseif (idx_JC >=6 && idx_JC <=15)
factor_k = 2.5
elseif (idx_JC >=16 && idx_JC <=25)
factor_k = 3.0
elseif (idx_JC >=26 && idx_JC <=40)
factor_k = 3.5
elseif (idx_JC >=41 && idx_JC <=50)
factor_k = 4.0
end
rCI_EM_km = factor_k .* 9.29e+05;
rCI_EM = rCI_EM_km / 1.496e+08;
mu = 3.04042342644015e-06;
% Circles data
xC = (mu-1);
yC = 0;
angle = 0:0.1:360;
circX = xC + rCI_EM*cosd(angle); circY = yC + rCI_EM*sind(angle);
% legend labels
k_value = ['$k =\ $',num2str(factor_k)];
r_CI_value = ['$r_{CI} =\ $',num2str(rCI_EM_km,'%3.2e'),'$\ km$'];
% Plot CI
CI_plot=plot(circX, circY,'Color',[0 1 0],'LineWidth',0.7,'DisplayName',r_CI_value);
% Plot Earth + Moon system
plot(mu -1,0,'.',...
'color',my_blue,'MarkerFaceColor',my_blue,'MarkerSize',20);
text(mu-1.0,0-0.0025,'$E/M$','Interpreter',"latex",'Color',my_blue);
%Legend
lgd = legend([UHIM_plots,CI_plot],'Orientation',"vertical",'Location',"northeast");
lgd.Interpreter = 'latex';
lgd.FontSize = 9;
% dim = [.24 .835 .150 .06];
%
% str = ['$JC\ index = $ ',num2str(idx_JC),newline,...
% '$No.\ traj.\ L_1 = $',num2str(numel([UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.x])/200),newline,...
% '$No.\ traj.\ L_2 = $',num2str(numel([UHIM_DB(idx_L).data(idx_JC).PLO_to_CI.x])/200)];
% annotation('textbox',dim,'String',str,'Interpreter',"latex",'FitBoxToText','on',...
% 'BackgroundColor',[1 1 1],'FontSize',8);
% Axes lables
xlabel(t,'$x$ (au)','interpreter','latex','fontsize',12); ylabel(t,'$y$ (au)','interpreter','latex','FontSize',12);
% Option to center better the curves inside the plot window
x_lim = xlim; y_lim = ylim;
xlim([x_lim(1)-0.0*norm(x_lim) x_lim(2)+0.0*norm(x_lim)]);
ylim([y_lim(1)-0.1*norm(y_lim) y_lim(2)+0.4*norm(y_lim)]);
title(k_value,'Interpreter','latex');
end
factor_k = 3
factor_k = 3.5000
factor_k = 3.5000
factor_k = 4
%exportgraphics(fig,['fig_UHIM_traj_CI_JC_id_',num2str(idx_JC),'.pdf'],'ContentType','vector')
%%save as pdf
%exportgraphics(fig,['fig_UHIM_traj_CI_JC_id_',num2str(idx_JC),'.pdf'],'ContentType','vector')
%%save as pdf
%exportgraphics(fig,['fig_UHIM_traj_CI_JC_id_',num2str(idx_JC),'.pdf'],'ContentType','vector')
%%save as pdf
.
  9 Comments
Star Strider
Star Strider on 9 Oct 2022
Thank you!
I did not entirely understand the latest problem, however I am happy that my approach using Position was part of the solution.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Oct 2022
I'm not seeing where you called nexttile. You need to do that.
  5 Comments

Sign in to comment.

Categories

Find more on Graphics Object Programming 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!

Translated by