# long dashes in a dashed line matlab plot

조회 수: 199 (최근 30일)
Dam 2012년 12월 30일
편집: Myrtle42 2020년 7월 30일
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
Dima
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Leo Simon 2015년 5월 29일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Jan 2012년 12월 30일
편집: Jan 2012년 12월 30일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Chad 2017년 12월 22일
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?

댓글을 달려면 로그인하십시오.

### 추가 답변 (4개)

Carl Howard 2017년 3월 29일
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.
x=[0:0.01:2*pi];
p1h=plot(x,sin(x),'k--',x,sin(x+pi/6),'k:',x,sin(x+pi/3),'k-.','LineWidth',1)
l1h=legend('Dash --','Dotted :','Dash Dot -.')
axis([0 7 -1 1]);
figno=gcf;
% Set the page size for the figure
set(figno, 'units', 'centimeters', 'pos', [0 0 10 8])
set(figno,'paperunits','centimeters')
set(figno,'paperposition',[0,0,10,8])
% Set some of the font properties
fontname ='Times New Roman';
hA=gca;
set(hA,'defaultaxesfontname',fontname);
set(hA,'defaulttextfontname',fontname);
set(hA,'fontname',fontname);
xlabel('This is the xlabel');
ylabel('This is the ylabel');
% make gridlines more visible
set(hA,'GridAlpha',0.8);
set(l1h,'Interpreter','Latex','FontSize',9,'FontName','Times New Roman');
set(hA,'FontUnits','points','FontWeight','normal', ...
'FontSize',9,'FontName','Times New Roman');
release=version('-release');
eps_file_output=['deleteme_',release,'.eps'];
%print -deps deleteme_2015a.eps
print(eps_file_output,'-deps')
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.
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Dam 2013년 1월 2일
Very useful answer .. I used the function dashline 'the last link' ..
Thank you very much
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Dam 2013년 1월 5일
But it s appearing in the legend as a solid line !!!!
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Jan 2013년 1월 5일
Yes, but this is another problem. There are a lot of functions in the FileExchange which allows to modify the legend.

댓글을 달려면 로그인하십시오.

Myrtle42 2020년 7월 30일
편집: Myrtle42 2020년 7월 30일
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

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by