How to precisely control line thickness in MATLAB plot?

Szymon Bęczkowski picture Szymon Bęczkowski · Apr 21, 2015 · Viewed 12.4k times · Source

I would like to precisely control the thickness of the line plotted in MATLAB. Not just 0.5, 1, 2, 3, ... points but e.g. 0.2mm. Is it possible?

There is a custom line scale and minimum line width box in the export setup window but that does not work.

enter image description here

Sample code:

hf = figure;
ha = axes;
ha.Units = 'centimeters';

t = linspace(0,2*pi);
hl = plot(t,sin(t),'Linewidth',0.1);
axis tight

saveas(hf,'test','pdf')

Answer

Carlos picture Carlos · Jul 22, 2015

MatLab uses the standard definition of 1 PostScript Point (or "Desktop Publishing Point") = 1/72 inches.

(You can confirm this easily by exporting a figure with, say, a line with 'LineWidth' equal to 36. If you print that without scaling, the line on the paper will be 1/2 inch wide)

So if you want a line of 0.2 mm, you can set the line width to 0.567 or so:

h = plot([0 0],[0 1]);
set(h,'LineWidth',0.567);

and if you want to set that as the default line width for all your plots:

 set(0,'defaultlinelinewidth',0.567)

for a single session, or put into your startup.m file to set it permanently.

In response to @szymon-bęczkowski: with 2014b and later, there seems to be a bug in Matlab that sets the linewidth to a minimum value of 1 when exporting to EPS or PDF. See here for a related bug. So the 'workaround' as it is, is to stick to linewidth>=1.

Although it doesn't seem to work there either, I strongly recommend export_fig as an alternative to Matlab's built it printing capabilities.