long dashes in a dashed line matlab plot

340 views (last 30 days)
Dam on 30 Dec 2012
Edited: Myrtle42 on 30 Jul 2020
Good morning, I m plotting some results with matlab using the plot command as follows: plot(X,'color','r','linestyle','--','linewidth',1.5) I wonder if it s possible to change the length of the dashes to use longer ones !! Thank you in advance & Happy new year
  1 Comment
Leo Simon
Leo Simon on 29 May 2015
Wondering if there's been any improvements in this regard in more recent versions of matlab? It would be really nice if there were a simpler way of custom specifying spacing for all but the non-default linestyles. Thanks! Leo

Sign in to comment.

Accepted Answer

Jan on 30 Dec 2012
Edited: Jan on 30 Dec 2012
For plotting on the screen, there is no straight solution. When you export the diagram to an EPS, there are some solutions in the FileExchange:
For the display on the screen you can use this workaround:
x = linspace(0, 4*pi, 400);
y = sin(x);
yDashed = y;
yDashed(10:20:length(yDashed)) = NaN;
plot(x, yDashed);
A nicer solution would be not to use equidistant steps on the X axis, but measure the curve length:
rangeX = max(x) - min(x);
rangeY = max(y) - min(y);
Q = [diff(x) / rangeX; diff(y) / rangeY];
L = [0, cumsum(sqrt(sum(Q .* Q)))];
L = (length(L) / L(end)) * L;
a = 5; % start point
b = 1; % gap width
c = 10; % gap frequency
index = rem(round(L) + a, c) <= b;
yDashed = y;
yDashed(index) = NaN;
plot(x, yDashed);
  1 Comment
Chad on 22 Dec 2017
Unfortunately, most of the FileExchange (MatlabCentral) functions are out of date and do not work. A real solution to the problem is necessary. In addition the legend issue which would need further manipulation, there seems to be major changes and issues with old methods I have a similar unanswered question https://www.mathworks.com/matlabcentral/answers/373983-how-do-i-create-custom-line-styles-for-simple-line-plots?

Sign in to comment.

More Answers (4)

Carl Howard
Carl Howard on 29 Mar 2017
There appears to be an issue with the way Matlab handles dashed lines between versions later than 2015a. Here is a Minimal Working Example (MWE) script to demonstrate the problem on a Windows 10, 64-bit version of Matlab.
%%Script to plot dash line to illustrate problem
% Versions of Matlab more recent that 2015a plot dashed lines differently.
l1h=legend('Dash --','Dotted :','Dash Dot -.')
axis([0 7 -1 1]);
% Set the page size for the figure
set(figno, 'units', 'centimeters', 'pos', [0 0 10 8])
% Set some of the font properties
fontname ='Times New Roman';
xlabel('This is the xlabel');
ylabel('This is the ylabel');
% make gridlines more visible
set(l1h,'Interpreter','Latex','FontSize',9,'FontName','Times New Roman');
set(hA,'FontUnits','points','FontWeight','normal', ...
'FontSize',9,'FontName','Times New Roman');
%print -deps deleteme_2015a.eps
If one uses ghostview to inspect the resulting line plots you get this for 2015a (left image) and 2016b (right).
The 2016b versions cannot be used, as the dash-dot -. and dotted : lines are poor and look like a solid line.
I tried several scripts on File Exchange that are meant to improve the dash line spacing by editing the .eps files, but these scripts search for /DO, /DA etc , postscript command that define the dash line style, but they do not appear in the 2015a and later postscript output, and therefore the scripts no longer work.
It seems the new way Matlab generates .eps commands for dashed lines is using the Postscript commands like "[10 6] 0 setdash". One can change the length of the solid line and the length of the space by playing around with these numbers in the .eps file.
If the LineWidth is increased in 2016a and 2016b from 1.0 to 1.5, this slightly improves the quality of the lines, but is not an option if you must have linewidths of 1.0 for publication purposes.
Alternatively, one can still use release 2015a, which generates reasonable plots.

Dam on 2 Jan 2013
Very useful answer .. I used the function dashline 'the last link' ..
Thank you very much

Dam on 5 Jan 2013
But it s appearing in the legend as a solid line !!!!
  1 Comment
Jan on 5 Jan 2013
Yes, but this is another problem. There are a lot of functions in the FileExchange which allows to modify the legend.

Sign in to comment.

Myrtle42 on 30 Jul 2020
Edited: Myrtle42 on 30 Jul 2020
I still couldn't find a solution to this so made a function to control dash length, at least for plotting straight lines. I'm sure it could be modified with Jan's solution for curves, but I only needed straight lines so kept it simple

Community Treasure Hunt

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

Start Hunting!

Translated by