How to change the linewidth of the axes without affecting the linewidth of the legend box?

160 views (last 30 days)
I have tried using
ax=gca;ax.LineWidth=2;
which makes both the axis lines and the legend box line thicker. Then I tried to set the legend box lines thinner with
hl = findobj(hobj,'type','line'); set(hl,'LineWidth',1.5);
but it makes both the axis lines and the legend box lines thinner

Accepted Answer

dpb
dpb on 21 Sep 2019
Edited: dpb on 21 Sep 2019
Use the two object handles to set their respective properties--although legend does inherit axes properties such as the linewidth from its parent axis which makes a certain amount of sense.
hAx=gca; % create an axes
hL=plot(randn(5,1)); % and a line underneath
hAx.LineWidth=2; % set the axis linewidth for box/ticks
hLg=legend('A'); % put a legend on the figure
hLg.LineWidth=1; % make the legend axes box linewidth smaller
Presuming hobj is the handle of the figure or returned by whichever function used to plot in the axes, hl above is the line in the graph equivalent to hL above. Setting 'LineWidth' for that object changes the linestyle of the line itself, not the same property at all as the axes. And, changing it will change the line characteristics in both as the legend reflects the characteristics of the associated line in the axes to which it belongs. That is a builtin connection that is immutable without trickery to create secondary lines such that the connection between which legend entry is shown and the given line is disguised by creating hidden objects or similar.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!