I am creating a plot in matlab with a grid, when I try to save it as a vector image the plot lines dissapear. Any idea how to solve this?
to save I am using
print(gcf,'-vector','-dsvg',['Page1Vector','.svg']) % svg
or
saveas(gcf,'Page1Report.svg')
neither is working. I attach a randerized version of the plot and how is saved as a vector
as asked, this is a script that recreates the problem:
x_fs=500;
acc1temp=200*sin(0.1.*(1:15000));
acc2temp=200*cos(0.1.*(1:15000));
acc3temp=200*sin(0.05.*(1:15000));
window1=5000;
f=figure;
f.Units='centimeters';
f.Position = [1 1 500 180];
ax = axes;
lw=0.00001;
grid(ax)
grid(ax, 'minor')
color = [ .8 ,.8, 1 ];
ax.GridColor = [ 0 0 1 ];
ax.GridAlpha = 0.2;
ax.MinorGridColor = [0 0 1];
ax.MinorGridAlpha = 0.1;
ax.MinorGridLineStyle= '-';
hold on
initialtime=0;
for i=1:3
acc1temp1=acc1temp((i-1)*window1+1:i*window1);
acc2temp1=acc2temp((i-1)*window1+1:i*window1);
acc3temp1=acc3temp((i-1)*window1+1:i*window1);
if i==1
loc1=acc3temp1+3600;
loc2=acc1temp1+3200;
loc3=acc2temp1+2800;
elseif i==2
loc1=acc3temp1+2300;
loc2=acc1temp1+1900;
loc3=acc2temp1+1500;
else
loc1=acc3temp1+1000;
loc2=acc1temp1+600;
loc3=acc2temp1+200;
end
timeline=1:5000;
timeline=timeline';
h2=plot(timeline,loc1,'k');
set(h2,'LineWidth',lw);
hold on
h3=plot(timeline,loc2,'k');
set(h3,'LineWidth',lw);
hold on
h4=plot(timeline,loc3,'k');
set(h4,'LineWidth',lw);
hold on
end
xlim([0, 5000])
set(gca,'xtick',[0:100:5000])
ylim([0,3900])
set(gca,'ytick',[0:100:3900])
set(gca,'Yticklabel',[])
set(gca,'Xticklabel',[])
set(gca,'XColor',color,'YColor',color)
set(gcf,'GraphicsSmoothing','off')
txt = {'Time= 0-10s'};
text(100,3850,txt)
txt = {'S'};
text(50,3670,txt)
txt = {'A'};
text(50,3270,txt)
txt = {'P'};
text(50,2870,txt)
txt = {'Time= 10-20s'};
text(100,2550,txt)
txt = {'S'};
text(50,2370,txt)
txt = {'A'};
text(50,1970,txt)
txt = {'P'};
text(50,1570,txt)
txt = {'Time= 20-30s'};
text(100,1250,txt)
txt = {'S'};
text(50,1070,txt)
txt = {'A'};
text(50,670,txt)
txt = {'P'};
text(50,270,txt)
axis equal
xlim([0, 5000])
ylim([0,3900])
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
saveas(gcf,'raster.png')
saveas(gcf,'vector1.svg')
print(gcf,'-vector','-dsvg',['vector2','.svg']) % svg
again the png show the sine waves while the svg only shows the grid ad the text but the sine waves disappear
0.00001 is an extremely small number for the line width.
My guess is that MATLAB renders the line with some minimal thickness, so on the screen and in the PNG file it looks good. But MATLAB also writes your chosen value to the SVG file, and whatever tool you use to render the SVG file honors your chosen line width.
And this is a very, very thin line. It is so thin you don't see it!
I increased the lw
value to 0.5
, which is still a pretty thin line. It looks good on the screen, in the PNG file, and in the SVG file.
From the documentation:
LineWidth — Line width
0.5 (default) | positive valueLine width, specified as a positive value in points, where 1 point = 1/72 of an inch. If the line has markers, then the line width also affects the marker edges.
The line width cannot be thinner than the width of a pixel. If you set the line width to a value that is less than the width of a pixel on your system, the line displays as one pixel wide.
MATLAB translates inches to pixels using your screen resolution, if you have a high-resolution display, you might have a pixel size of 1/150 of an inch or so, which is half a point. Anything less than that will be smaller than a pixel, and so the line will look thicker on screen than in the SVG file.