Is it possible to change dash pattern in vector plot?

11 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 29 Ago 2024
Modificato: dpb il 31 Ago 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.
Siucheuk Kam
Siucheuk Kam il 1 Set 2024
I've tried using exportgraphics/print/saveas, the result is pretty much the same.
Following are codes to export a dashed line to png(non-vector) and pdf(vector).
clear;
clc;
close all;
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);
a=plot(xx, yy, '-k', 'LineWidth', 0.8, 'DisplayName', 'Constant Slip');
box on
grid on
grid minor
l = legend('Location', 'northoutside', 'Orientation', 'vertical');
xlabel('Time (min)')
ylabel('\it{F/F}_{\rm{est}}')
xticks(0:3:12)
xlim([0, 12])
yticks([0, 0.1, 0.4, 0.7, 1.0])
ytickformat('%.1f')
ylim([0, 1.1])
hold off
set(gca, 'FontName', 'Times New Roman', 'FontSize', 10.5);
pngName = 'EN26891.png';
vecName = 'EN26891.pdf';
exportgraphics(f, pngName, 'Resolution', '600');
exportgraphics(f, vecName, 'ContentType', 'vector');

Accedi per commentare.

Risposte (1)

Rushikesh
Rushikesh il 4 Set 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.

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by