MATLAB Answers

long dashes in a dashed line matlab plot

654 ビュー (過去 30 日間)
Dam
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 件のコメント

Leo Simon
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
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 件のコメント

Chad
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 件)

yingwee
yingwee 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 件のコメント

サインインしてコメントする。


Dam
Dam 2013 年 1 月 2 日
Very useful answer .. I used the function dashline 'the last link' ..
Thank you very much

  0 件のコメント

サインインしてコメントする。


Dam
Dam 2013 年 1 月 5 日
But it s appearing in the legend as a solid line !!!!

  1 件のコメント

Jan
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
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

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by