Effacer les filtres
Effacer les filtres

How to break a single horizontal line in the legend of a Matlab plot into two horizontal lines?

17 vues (au cours des 30 derniers jours)
I have a legend as follows:` lg=legend([pl_1, pl_6, pl_s_1, pl_s_6, pl_s_3, pl_s_7], 'disc (impuls)', 'disc (step)', 'stoch1 (impuls)', 'stoch1 (step)', 'stoch (impuls)', 'stoch (step)', 'interpreter','latex','fontsize',34,'orientation','horizontal','position',[0.12 0.04 0.8 0.034]); The legend with six items is too long, and I've attempted to split it into two lines, with the first four items in the first line and the remaining two in the second line. However, despite trying various approaches, the legend remains on a single line without any breaks. I wonder how to resolve this issue.
For additional details, I've listed the options I've attempted below:
#Case 1
legend([pl_1, pl_6, pl_s_1, pl_s_6, pl_s_3, pl_s_7], ... 'disc (impuls)', 'disc (step)', 'stoch (impuls)', 'stoch (step)', ... 'stoch1 (impuls)\newline', 'stoch (step)', ... 'interpreter', 'latex', 'fontsize', 34, 'orientation', 'horizontal', ... 'position', [0.12 0.04 0.8 0.034]);
--------------------------------------------------------------------------------------------
#Case 2
% Create the legend
legend([pl_1, pl_6], {'disc (impuls)', 'disc (step)', ... 'stoch (impuls)', 'stoch (step)', 'stoch1 (impuls)', 'stoch (step)'}, ... 'interpreter', 'latex', 'fontsize', 34, 'orientation', 'horizontal', ... 'position', [0.12 0.04 0.8 0.034]);
% Modify the legend to split the line
lgd = legend('Location', 'southoutside'); lgd.String{5} = 'stoch1 (impuls)'; lgd.String{6} = 'stoch (step)';
--------------------------------------------------------------------------------------------
#Case 3
% Create a legend using text objects
lgd_pos = [0.12, 0.04, 0.8, 0.034];
lgd_text = {'disc (impuls)', 'disc (step)', 'stoch (impuls)', 'stoch (step)'};
lgd_text{end+1} = 'stoch1 (impuls)';
lgd_text{end+1} = 'stoch (step)';
lgd_handles = zeros(1, numel(lgd_text));
for i = 1:numel(lgd_text)
lgd_handles(i) = text(lgd_pos(1) + (i - 1) * lgd_pos(3) / numel(lgd_text), ... lgd_pos(2), lgd_text{i}, 'Interpreter', 'latex', 'FontSize', 34, 'HorizontalAlignment', 'center');
end
% Remove the default legend
legend off;
% Set the legend position manually
legend(lgd_handles, lgd_text, 'Interpreter', 'latex', 'FontSize', 34, 'Orientation', 'horizontal', 'Position', lgd_pos);
------------------------------------------------------------------------------------
#Case 4
% Create the legend with LaTeX markup legend([pl_1, pl_6], {'disc (impuls)', 'disc (step)', 'stoch (impuls)', 'stoch (step)', 'stoch1 (impuls) \\newline stoch (step)'}, 'interpreter', 'latex', 'fontsize', 34, 'orientation', 'horizontal', 'position', [0.12 0.04 0.8 0.034]);
-------------------------------------------------------------------------------------------------
#Case 5
% Create the first part of the legend
legend([pl_1, pl_6, pl_s_1, pl_s_6, pl_s_3, pl_s_7], {'disc (impuls)', 'disc (step)', ... 'stoch (impuls)', 'stoch (step)'}, 'interpreter', 'latex', 'fontsize', 34, 'orientation', 'horizontal',... 'position', [0.12 0.04 0.8 0.034]);
% Create the second part of the legend below the first one
lgd_text = {'stoch1 (impuls)', 'stoch (step)'}; lgd_pos = get(legend, 'Position');
% Get the position of the first legend
lgd_pos(2) = lgd_pos(2) - 0.06;
% Adjust the vertical position
legend(lgd_text, 'interpreter', 'latex', 'fontsize', 34, 'orientation', 'horizontal', 'position', lgd_pos);
-----------------------------------------------------------------------------------
#Case 6
% Create the legend with LaTeX markup
lgd = legend([pl_1, pl_6, pl_s_1, pl_s_6, pl_s_3, pl_s_7], {'disc (impuls)', 'disc (step)', 'stoch (impuls)', 'stoch (step)', 'stoch1 (impuls) stoch (step)'}, 'Interpreter', 'latex', 'fontsize', 34, 'orientation', 'horizontal', 'position', [0.12 0.04 0.8 0.034]);
% Create a second line of text with a line break
lgd_text = 'stoch1 (impuls) \newline stoch (step)';
text(lgd.Position(1), lgd.Position(2) - 0.03, lgd_text, 'Interpreter', 'latex', 'FontSize', 34, 'HorizontalAlignment', 'center');

Réponse acceptée

Voss
Voss le 4 Sep 2023
legend([pl_1, pl_6, pl_s_1, pl_s_6, pl_s_3, pl_s_7], ...
    {'disc (impuls)', 'disc (step)', 'stoch1 (impuls)', 'stoch1 (step)', 'stoch (impuls)', 'stoch (step)'}, ...
    'interpreter','latex', ...
    'fontsize',34, ...
    'NumColumns',4, ...
    'Orientation','horizontal')

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by