How to place a subplot with the same dimensions as ax.width, ax.height and ax.Position
1 次查看(过去 30 天)
显示 更早的评论
I am plotting a graph and I would like the start of the subplot(3,1,1) to coincide with the start of the subplot (3,1,2) and (3,1,3), as well as its ends, height and sizes. The code I am using is below:
% Plot em função do TEMPO (FASE A)
figure(1)
subplot(3,1,1);plot(T,Ida,'color',verde_unipampa,'LineWidth',1.5);xlim([Ti Tf]);
title('(a)','FontName','Times New Roman','FontSize',15);
set(gca,'fontname','Times New Roman')
grid on
hold on
xline(instanteChaveamento,'r--','LineWidth',1.5)
xlabel('Tempo (s)','FontName','Times New Roman','FontSize',15);
ylabel('Ida (A)','FontName','Times New Roman','FontSize',15);
lgnd = legend('Ida','Chaveamento');
set(lgnd,'FontName','Times New Roman','FontSize',15)
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2)+0.05;
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4)-0.02;
ax.Position = [left bottom ax_width ax_height];
%---------------------------------------------------------------------------
% Ajustando o subplot 2 com o mesmo estilo do subplot 1
subplot(3,1,2); % Seleciona a segunda parte para o próximo gráfico
stairs(tempos, FI_A, 'Color', verde_unipampa, 'LineWidth', 1.5); % Plota FI_AxT
xlim([Ti, Tf]); % Ajusta os limites do eixo x conforme definido para o primeiro plot
title('(b)', 'FontName', 'Times New Roman', 'FontSize', 15); % Note o título ajustado para '(b)'
set(gca, 'FontName', 'Times New Roman');
grid on; % Ativa a grade
hold on;
xline(instanteChaveamento, 'r--', 'LineWidth', 1.5); % Linha de chaveamento
xlabel('Tempo (s)', 'FontName', 'Times New Roman', 'FontSize', 15);
ylabel('FI_A', 'FontName', 'Times New Roman', 'FontSize', 15);
lgnd = legend('FI_A', 'Chaveamento');
set(lgnd, 'FontName', 'Times New Roman', 'FontSize', 15);
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2) + 0.05;
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4) - 0.02;
ax.Position = [left bottom ax_width ax_height];
%--------------------------------------------------------------------------
subplot(3,1,3)...
But I'm not getting the expected result, as shown in the image the beginning of the ax (red line) does not coincide. I would like to correct this.
0 个评论
采纳的回答
Voss
2024-2-19
I gather that you want to perform an adjustment to the positions of your axes, increasing the y (bottom) by 0.05 and decreasing the height by 0.02, but when you do so the x (left) are no longer aligned.
To do this, I wouldn't use OuterPosition and TightInset, but instead just adjust Position directly. Something like as follows:
% made up data to plot:
Ti = 0;
Tf = 1;
T = linspace(Ti,Tf);
Ida = rand(1,100);
verde_unipampa = [0 1 0];
tempos = T;
FI_A = rand(1,100);
instanteChaveamento = 0.1;
% Plot em função do TEMPO (FASE A)
figure
ax = subplot(3,1,1);
plot(T,Ida,'color',verde_unipampa,'LineWidth',1.5);xlim([Ti Tf]);
title('(a)','FontName','Times New Roman','FontSize',15);
set(gca,'fontname','Times New Roman')
grid on
hold on
xline(instanteChaveamento,'r--','LineWidth',1.5)
xlabel('Tempo (s)','FontName','Times New Roman','FontSize',15);
ylabel('Ida (A)','FontName','Times New Roman','FontSize',15);
lgnd = legend('Ida','Chaveamento');
set(lgnd,'FontName','Times New Roman','FontSize',15)
%---------------------------------------------------------------------------
% Ajustando o subplot 2 com o mesmo estilo do subplot 1
ax(2) = subplot(3,1,2); % Seleciona a segunda parte para o próximo gráfico
stairs(tempos, FI_A, 'Color', verde_unipampa, 'LineWidth', 1.5); % Plota FI_AxT
xlim([Ti, Tf]); % Ajusta os limites do eixo x conforme definido para o primeiro plot
title('(b)', 'FontName', 'Times New Roman', 'FontSize', 15); % Note o título ajustado para '(b)'
set(gca, 'FontName', 'Times New Roman');
grid on; % Ativa a grade
hold on;
xline(instanteChaveamento, 'r--', 'LineWidth', 1.5); % Linha de chaveamento
xlabel('Tempo (s)', 'FontName', 'Times New Roman', 'FontSize', 15);
ylabel('FI_A', 'FontName', 'Times New Roman', 'FontSize', 15);
lgnd = legend('FI_A', 'Chaveamento');
set(lgnd, 'FontName', 'Times New Roman', 'FontSize', 15);
% %--------------------------------------------------------------------------
% ax(3) = subplot(3,1,3)...
% ---- axes Position adjustment ----
drawnow
ax_pos = get(ax,'Position');
ax_pos = vertcat(ax_pos{:});
% adjust y and height:
ax_pos(:,[2 4]) = ax_pos(:,[2 4])+[0.05 -0.02];
% if necessary, you can explicitly make x the same and width the same for
% all axes:
% ax_pos(:,1) = max(ax_pos(:,1));
% ax_pos(:,3) = min(ax_pos(:,3));
set(ax,{'Position'},num2cell(ax_pos,2))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Labels and Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!