long dashes in a dashed line matlab plot
169 次查看(过去 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
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:
- http://www.mathworks.com/matlabcentral/fileexchange/23604-fixlines
- http://www.mathworks.com/matlabcentral/fileexchange/17928-fixpslinestyle
- http://www.mathworks.com/matlabcentral/fileexchange/15743-fix-dashed-and-dotted-lines-in-eps-export
- http://www.mathworks.com/matlabcentral/fileexchange/1892-dashline
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
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173567/image.png)
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 个评论
另请参阅
类别
在 Help Center 和 File 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!