Is it possible to change dash pattern in vector plot?
11 views (last 30 days)
Show older comments
Trying to export dashed plot to vertor file (emf, svg, pdf).
Ran into an issue where the dash pattern seems strange (space is too big?), while the pattern in png plot is normal.
Left: emf file. Right png file.
2 Comments
dpb
on 29 Aug 2024
Edited: dpb
on 31 Aug 2024
The only prepackaged <linestyle choices> offered by plot are the limited few and there's no adjustment features with them other than width and color.
To get anything else, you'd have to draw the line sections individually setting the length and spacing between them.
How, specifically, did you export the figures? There are multiple possible ways; some better than others...I have little recent experience trying to build publish-quality figures and the toolsets have changed since, but "show your work" so others with such experience can see/comment.
Making it simple for others to do so would be to upload your code/data as a minimum working example.
Answers (1)
Rushikesh
on 4 Sep 2024
I understand that you are experiencing different dash patterns in PNG and EMF/PDF (vector) files. Based on my understanding of vector files, line styles such as dashes are defined in terms of relative units (points or inches). This can lead to variations when the graphic is rendered or scaled, as the rendering engine used to display the EMF may interpret these units differently compared to how MATLAB renders them on screen.
In contrast, for PNG files, the line style is converted to a fixed pixel pattern. This means the appearance on screen and in the exported image is consistent because it is rendered as a bitmap at the time of export.
In summary, when using an EMF file, the dashed line pattern is preserved in a scalable format, whereas in a PNG file, the dash pattern is fixed in pixels. As a result, EMF files may scale differently depending on the rendering software, and this behaviour is independent of MATLAB.
One way to achieve similar patterns in both formats is to manually plot segments of lines and gaps in between. You can refer to the code below to get an idea of how to manually plot lines:
f = figure("Name", 'Loading Protocal', 'Units', 'centimeters', 'Position', [1 1 7 6], 'Color', 'w');
hold on
plot([0, 2, 2.5, 4, 4.5, 7.5], [0, 0.4, 0.4, 0.1, 0.1, 0.7], '-k', 'LineWidth', 0.8, 'DisplayName', 'Constant Load')
x = [7.5, 9.5, 11];
y = [0.7, 1.0, 1.05];
cs = spline(x, [0.2, y, 0]);
xx = linspace(7.5, 11, 10);
yy = ppval(cs, xx);
dashLength = 0.1;
gapLength = 0.05;
totalLength = 0; % Initialize total length traveled along the curve
% Plot the dashed spline manually
for i = 1:length(xx)-1
% Calculate the distance between current and next point
segmentLength = sqrt((xx(i+1) - xx(i))^2 + (yy(i+1) - yy(i))^2);
totalLength = totalLength + segmentLength;
% Check if current segment should be a dash
if mod(totalLength, dashLength + gapLength) < dashLength
plot(xx(i:i+1), yy(i:i+1), '-k', 'LineWidth', 0.8, 'DisplayName', 'Constant Slip');
end
end
box on
grid on
grid minor
xlabel('Time (min)')
ylabel('\it{F/F}_{\rm{est}}')
hold off
set(gca, 'FontName', 'Times New Roman', 'FontSize', 10.5);
pngName = 'EN26891-2.png';
vecName = 'EN26891-2.pdf';
exportgraphics(f, pngName, 'Resolution', '300');
exportgraphics(f, vecName, 'ContentType', 'vector');
Here are the results when ran on R2024a. Left side is PNG file and right side is PDF file.
I hope this resolves the issue you had, and you can proceed further.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!