long dashes in a dashed line matlab plot
318 views (last 30 days)
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
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;
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;
More Answers (4)
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');
'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.
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