Reduce Legend line size and box accordingly
Afficher commentaires plus anciens
This code generates a plot that I would put directly on a paper. I have troubles reducing the legend lines length and legend box accordingly such that it does appear nicer. Any input is appreciated. Thank you.
if true
f = 1e3;
t_step = 1/f/100;
t = [0:t_step:3/f];
A0 = 1;
B0 = 3;
a = A0*sin(2*pi*f*t);
b = B0*cos(2*pi*f*t);
plot_scale = 1;
% plot size and position in centimeter
plot_width = 6.6*plot_scale;
plot_height = 4.5*plot_scale;
plot_position_x = 7.0;
plot_position_y = 20.0;
% window figure title
window_title = 'Test Figure';
% axis label (LaTeX syntax)
x_label = 'time - $t$ (ms)';
y_label = '(V)';
% axis limit
x_limit = [0 3];
y_limit = [-4 4];
% axis number of tick
x_tick = 2;
y_tick = 3;
x_tick = x_tick+2;
y_tick = y_tick+2;
% legend entries (LaTeX syntax)
y1_legend_entry = '$v_\mathrm{in}$';
y2_legend_entry = '$v_\mathrm{out}$';
% font size
font_size_label = 9;
font_size_legend = font_size_label;
% filename
filename = 'test_figure';
% color
color(1,:) = [0.0000 0.4470 0.7410];
color(2,:) = [0.8500 0.3250 0.0980];
%-------------------------------------------------------------------------%
%figure
figure('Name', window_title, 'Color', 'White', 'Units', 'centimeter', 'Position', [plot_position_x plot_position_y plot_width plot_height]);
hold on;
h_y1 = plot(t*1e3, a, '-', 'Color',color(1,:), 'LineWidth',1);
h_y2 = plot(t*1e3, b, '-', 'Color',color(2,:), 'LineWidth',1);
h2_y1 = plot(t(10:10:(end-10))*1e3, a(10:10:(end-10)), 'o', 'Color',color(1,:), 'MarkerSize',2, 'MarkerFaceColor',color(1,:));
h_x_label = xlabel(x_label, 'Interpreter','latex');
h_y_label = ylabel(y_label, 'Interpreter','latex');
h_legend = legend([h_y1, h_y2], y1_legend_entry,y2_legend_entry, 'Location','NorthEast');
set(h_legend, 'Interpreter','latex');
set([h_legend, gca], 'FontSize',font_size_legend);
set([h_x_label, h_y_label, gca], 'FontSize',font_size_label);
set(gca, ...
'Box' , 'on' , ...
'BoxStyle' , 'back' , ...
'TickDir' , 'in' , ...
'TickLength' , 0.01*[1 1] , ...
'XScale' , 'linear' , ...
'YScale' , 'linear' , ...
'XLim' , x_limit , ...
'YLim' , y_limit , ...
'XTick' , linspace(x_limit(1), x_limit(2), x_tick), ...
'YTick' , linspace(y_limit(1), y_limit(2), y_tick), ...
'TickLabelInterpreter', 'latex' , ...
'XMinorTick' , 'on' , ...
'YMinorTick' , 'on' , ...
'XGrid' , 'on' , ...
'YGrid' , 'on' , ...
'XMinorGrid' , 'off' , ...
'YMinorGrid' , 'off' , ...
'GridLineStyle' , '-' , ...
'GridColor' , 0.3*[1 1 1] , ...
'GridAlpha' , 0.5 , ...
'MinorGridLineStyle' , '-' , ...
'MinorGridColor' , 0.1*[1 1 1] , ...
'MinorGridAlpha' , 0.5 , ...
'XColor' , 'k' , ...
'YColor' , 'k' , ...
'LineWidth' , 0.35 );
set(gcf, 'PaperPositionMode', 'auto');
end
Réponses (1)
To reduce the legend lines length and adjust the legend box, you can modify the following lines of code:
- Add the `'AutoUpdate','off'` property to the `legend` function to prevent it from automatically updating when the plot changes.
h_legend = legend([h_y1, h_y2], y1_legend_entry, y2_legend_entry, 'Location', 'NorthEast', 'Interpreter', 'latex', 'AutoUpdate', 'off');
- Adjust the `LineWidth` property of the legend lines to make them shorter. For example, you can set it to `0.5`:
set(h_legend, 'LineWidth', 0.5);
- Adjust the size of the legend box by modifying the `Position` property of the legend. You can experiment with different values to achieve the desired result. For example:
legend_position = get(h_legend, 'Position');
legend_position(3) = legend_position(3) * 0.8; % Reduce the width of the legend box by 20%
set(h_legend, 'Position', legend_position);
Catégories
En savoir plus sur Legend dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!