Search code examples
matlabmatlab-figure

Matlab line plot: is it possible to set marker line width separately?


Is there a way to set the marker line width separately in a Matlab plot?

If we use 'LineWidth', it will change the width of both the marker edge and the line itself. We can plot marker and the line in two separate plot functions, however, there will be a problem in setting the legend.


Solution

  • This appears to be possible, but it is undocumented.

    Line objects have a hidden property MarkerHandle which references a Marker object representing the drawn markers; this object has its own LineWidth property. Once the markers have been drawn, the Marker object's LineWidth property affects the marker line width independently from the LineWidth property on the main Line object. This allows you to do:

    hLine = plot(myData, 'Marker',myMarkerType, 'LineWidth',myMainLineWidth);
    drawnow;
    hLine.MarkerHandle.LineWidth = myMarkerLineWidth;
    

    However this property doesn't seem to affect the line's entry in a legend. This, too, is accessible through undocumented properties though.

    hLegend = legend;
    drawnow;
    lineEntry = findobj(hLegend.EntryContainer, 'Object',hLine);
    entryMarker = findobj(lineEntry.Icon.Transform, 'Description','Icon Marker');
    entryMarker.LineWidth = myMarkerLineWidth;
    

    This works for me in MATLAB R2018b, but since it's not documented there are no guarantees the result will behave exactly as you expect. In particular, you might need to look out for your manual changes being overwritten automatically by other updates you make to your plot using documented features which cause the markers to be redrawn.