将 MATLAB 图保存为 svg 矢量图像时,绘图线消失

问题描述 投票:0回答:1

我正在 matlab 中使用网格创建一个绘图,当我尝试将其保存为矢量图像时,绘图线消失了。知道如何解决这个问题吗?

为了保存我正在使用

print(gcf,'-vector','-dsvg',['Page1Vector','.svg']) % svg

saveas(gcf,'Page1Report.svg')

两者都不起作用。我附上了绘图的随机化版本以及如何保存为矢量

按照要求,这是一个重现问题的脚本:

    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

png再次显示正弦波,而svg仅显示网格广告文本,但正弦波消失

matlab plot matlab-figure vector-graphics
1个回答
0
投票

0.00001 对于线宽来说是一个极小的数字。

我的猜测是,MATLAB 以最小的粗细渲染线条,因此在屏幕和 PNG 文件中看起来不错。但 MATLAB 还会将您选择的值写入 SVG 文件,并且无论您使用什么工具渲染 SVG 文件,都会遵循您选择的线宽。

这是一条非常非常细的线。太薄了你看不到它!

我将

lw
值增加到
0.5
,这仍然是一条非常细的线。它在屏幕、PNG 文件和 SVG 文件中看起来都不错。


来自文档

线宽,指定为以磅为单位的正值,其中 1 磅 = 1/72 英寸。如果线条有标记,则线条宽度也会影响标记边缘。

线宽不能比像素的宽度细。如果您将线宽设置为小于系统上像素宽度的值,则该线将显示为一个像素宽。

MATLAB 使用您的屏幕分辨率将英寸转换为像素,如果您有高分辨率显示器,则像素大小可能为 1/150 英寸左右,即半个点。任何小于该值的值都将小于像素,因此屏幕上的线条看起来会比 SVG 文件中的线条更粗。

© www.soinside.com 2019 - 2024. All rights reserved.