How to precisely control line thickness in MATLAB plot?
Asked Answered
M

1

8

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')
Mirthless answered 21/4, 2015 at 18:20 Comment(6)
Short answer: it is not possible. Suggestion: export as vector graphic and adjust the linewidth in post processing. Another suggestion: well, you specify points and you can control figure size in pixels as well es the resolution for exporting. You could try to create a workaround around that.Fastback
Depending on the rest of your workflow you might find it preferable to use a different tool - LaTeX users have TikZ as an option and the matlab2tikz file exchange script is popular: mathworks.com/matlabcentral/fileexchange/… You can also pass named parameters, which could help with automation tex.stackexchange.com/a/120151Poulos
According to this official documentation uk.mathworks.com/help/symbolic/mupad_ref/linewidth.html It ought to be possible to use LineWidth = 0.3*unit::mmPetiolule
But this only works with the MuPAD and not with the normal plots.Ajar
Which version of Matlab are you using?Teleost
At the time of posting 2014bAjar
D
6

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.

Did answered 22/7, 2015 at 5:50 Comment(2)
Does this actually work? I know I can set anything less than 1pt (see my sample code, I use 0.1) but the line is not thinner on the output plot.Ajar
@SzymonBęczkowski that's great to hear.Did

© 2022 - 2024 — McMap. All rights reserved.