MATLAB Answers

Change linewidth of marker in legend

10 views (last 30 days)
Max Power
Max Power on 16 May 2021
Commented: Adam Danz on 16 May 2021
Hi there,
I would like to change the linewidth of the marker independent from the linewidth of the plotted solid line. By now (see code) I am able to change it in the plot itself. The legend however is a problem. When I export the figure as a .eps file at the end of the script, using the print() function, the linewidth of the marker is always changing back (to the unwanted thicker linewidth) in the legend.
Can anybody please help me to fix my problem?
close all
clear
clc
figure(1)
p1 = plot([1,2,3,4,5,6],[5,4,6,2,4,9],'Marker','o','MarkerSize',12,'MarkerEdgeColor','r', 'LineWidth',3);
hold on
p2 = plot([1,2,3,4,5,6],[9,1,3,4,7,7], 'Marker','+','Markersize',12,'markeredgecolor','r', 'LineWidth',4);
lgd = legend([p1 p2],{'data1','data2'});
drawnow
p1.MarkerHandle.LineWidth = 1.0;
lineEntry = findobj(lgd.EntryContainer, 'Object',p1);
entryMarker = findobj(lineEntry.Icon.Transform, 'Description','Icon Marker');
entryMarker.LineWidth = 1.0;
print(figure(1),'output_fig1','-depsc','-r300')
  1 Comment
Adam Danz
Adam Danz on 16 May 2021
Not only does the legend change when printing your figure but the legend reverts back to default behavior any time the content of the axes changes. For example, add another line obj on the axes and you'll see that the legend reverts to default: plot(4,5,'kx').
Setting autoupdate to off solves that but does not solve the reversion during printing lgd.AutoUpdate = 'off';

Sign in to comment.

Answers (1)

VBBV
VBBV on 16 May 2021
Edited: VBBV on 16 May 2021
close all
clear
clc
figure(1)
p1 = plot([1,2,3,4,5,6],[5,4,6,2,4,9],'Marker','o','MarkerSize',2,'MarkerEdgeColor','r', 'LineWidth',3);
hold on
p2 = plot([1,2,3,4,5,6],[9,1,3,4,7,7], 'Marker','+','Markersize',2,'markeredgecolor','r', 'LineWidth',4);
lgd = legend([p1 p2],{'data1','data2'});
drawnow
p1.LineWidth = 1; % change this line
p2.LineWidth = 1.5; % change this line
lineEntry = findobj(lgd.EntryContainer, 'Object',p1);
entryMarker = findobj(lineEntry.Icon.Transform, 'Description','Icon Marker');
entryMarker.LineWidth = 1.0;
print(figure(1),'output_fig1','-depsc','-r300')
change this line
p1.LineWidth = 1; % change this line
  2 Comments
Max Power
Max Power on 16 May 2021
Sorry VBBV but i think you got me wrong.
Let's forget about the red line (=p2).
I would like to have the linewidth of the solid blue line (=p1) set to 3.0. Apart from that the linewidth of the circle marker sould be 1.0!
If you run my code you will see that the plot itself (solid line and circle marker) retain the linewidth set.
On the other hand, if you take a closer look on the legend you will notize that the linewidth of the circle marker changes from 1.0 to 3.0 after the print() function is called.
Just by calling the print() function the correct linewidth (1.0) of the circles in the legend than changes to (3.0) and in the .eps file the wrong linewidth is depicted.
Can you help me, so that the correct linewidth can be observed in the .eps output-file?

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!