Saving figure as .svg alters appearance (2024)

38 views (last 30 days)

Show older comments

Marleen about 19 hours ago

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance

Commented: Marleen 16 minutes ago

Accepted Answer: Milan Bansal

  • sems.mat
  • means.mat

Open in MATLAB Online

Hi,

I'm making figures in MATLAB and want to export them in .svg to work with them in other programs. So far, that has been no problem, but I'm stumbling into something odd now. I'm trying to export this figure:

Saving figure as .svg alters appearance (2)

As you can see, this works fine in .png. This is also what the figure looks like in MATLAB. However, when I export as .svg, the blue patch that I plotted around the line to indicate the SEM continues until 10 seconds instead of 5:Saving figure as .svg alters appearance (3)

I think this might be because I'm first plotting the patch, and then alter the x axis. However, this is not visible in the matlab figure, and the red and green patches show no problem for this.

My code is as follows:

ROIs = {'VisualROI_R','SensoryROI_R','MotorROI_R','RetrosplenialROI_R',...

'VisualROI_L','SensoryROI_L','MotorROI_L','RetrosplenialROI_L'};

xaxeslimits = [-5 5];

f = figure('InvertHardcopy','off','Color',[1 1 1]);

t = tiledlayout(size(ROIs,2)/2, 2);

x = linspace(-5, 10, 226);

tileind = 2;

for roi = 1:size(ROIs, 2)

nexttile(tileind)

title(ROInames{roi})

eval(['yhbo = means.hbo.' ROIs{roi} ';'])

eval(['SEMhbo = sems.hbo.' ROIs{roi} ';'])

eval(['yhbr = means.hbr.' ROIs{roi} ';'])

eval(['SEMhbr = sems.hbr.' ROIs{roi} ';'])

eval(['y = means.fluo.' ROIs{roi} ';'])

eval(['SEM = sems.fluo.' ROIs{roi} ';'])

y = y-1; % to get centered around 0

% HbO

yyaxis right

plot(x, yhbo, 'Color', 'red', 'LineWidth', 2)

patch([x, fliplr(x)], [yhbo + SEMhbo fliplr(yhbo - SEMhbo)], 'r' ,'EdgeColor','none', 'FaceAlpha',0.25)

hold on

% HbR

plot(x, yhbr, 'Color', 'blue', 'LineStyle', '-', 'LineWidth', 2)

patch([x, fliplr(x)], [yhbr + SEMhbr fliplr(yhbr - SEMhbr)], 'b' ,'EdgeColor','none', 'FaceAlpha',0.25)

ylim([-1.5 1.5]);

h_label = ylabel('\Delta \muM', 'interpreter', 'Tex', 'Rotation', 270);

ax = gca;

ax.YColor = 'red';

ax.XColor = 'k';

set(ax, 'FontSize', 15, 'LineWidth', 2)

xlim(xaxeslimits);

% Fluo

yyaxis left

plot(x,y, 'Color', [0.4660 0.6740 0.1880], 'LineWidth', 2);

patch([x, fliplr(x)], [y + SEM fliplr(y - SEM)], 'g' ,'EdgeColor','none', 'FaceAlpha',0.25)

f_label = ylabel('\Delta F/F');

ylim([-0.05 0.05]); % centered at 0

ax = gca;

ax.YColor = [0.4660 0.6740 0.1880];

ax.XColor = 'k';

set(ax, 'FontSize', 15, 'LineWidth', 2)

xlabel('Time (sec)')

xlim(xaxeslimits);

if tileind == size(ROIs, 2)

tileind = 1;

else

tileind = tileind+2;

end

end

leg1 = legend({'GCaMP', 'SEM', 'HbO','SEM', 'HbR','SEM'}, 'Orientation', 'Horizontal');

leg1.Location = 'southoutside';

% f.Position = [10 10 1800 1000];

f.Position = [10 10 900 1000];

% save

% saveas(gcf, [SaveDir '/NVC/Sham_RS/' Acq '_' type '_' ROIsavename '_AvCurves.tiff'], 'tiff');

% saveas(gcf, [SaveDir '/NVC/Sham_RS/' Acq '_' type '_' ROIsavename '_AvCurves.eps'], 'epsc');

saveas(gcf, [SaveDir '/NVC/Sham_RS/' Acq '_' type '_' ROIsavename '_AvCurves.svg'], 'svg');

close(f)

Does anybody know how to fix this? I'd like to be able to easily alter the x-axis and have it still work.

Best,

Marleen

4 Comments

Show 2 older commentsHide 2 older comments

Adam Danz less than a minute ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180931

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180931

@Marleen please contact tech support and include instructions how to reproduce the problem. Feel free to include the URL of this thread.

Marleen 31 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180956

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180956

@Adam Danz Done! Thanks :)

Stephen23 27 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180971

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180971

Edited: Stephen23 11 minutes ago

Open in MATLAB Online

  • sems.mat
  • means.mat

Note that using EVAL to access structure fields has been deprecated for around twenty years:

https://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/

https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html

load means.mat

load sems.mat

ROIs = {'VisualROI_R','SensoryROI_R','MotorROI_R','RetrosplenialROI_R','VisualROI_L','SensoryROI_L','MotorROI_L','RetrosplenialROI_L'};

roi = 1;

Replace this anti-pattern evil EVAL:

eval(['yhbo = means.hbo.' ROIs{roi} ';'])

with this simpler, more efficient, recommended dynamic fieldname:

yhbo_better = means.hbo.(ROIs{roi})

yhbo_better = 1x226

-0.2080 -0.2100 -0.2119 -0.2138 -0.2156 -0.2173 -0.2189 -0.2204 -0.2217 -0.2230 -0.2243 -0.2257 -0.2274 -0.2293 -0.2317 -0.2345 -0.2380 -0.2420 -0.2465 -0.2515 -0.2570 -0.2629 -0.2692 -0.2759 -0.2831 -0.2906 -0.2986 -0.3068 -0.3150 -0.3229

<mw-icon class=""></mw-icon>

<mw-icon class=""></mw-icon>

Checking:

isequal(yhbo,yhbo_better)

ans = logical

1

Marleen 16 minutes ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3181036

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3181036

@Stephen23 Haha I was wondering when someone would comment about my mediocre coding skills! Thanks for letting me know, I have a lot of those and it does read horribly, so this will improve my code a lot

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Milan Bansal about 8 hours ago

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#answer_1468211

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#answer_1468211

Edited: Milan Bansal about 8 hours ago

Open in MATLAB Online

Hi Marleen,

The issue with the blue patch extending beyond the intended x-axis limit when exporting to .svg can be addressed by ensuring that the data used to create the patch strictly adheres to the x-axis limits you set.

In your code, the x-axis limits are set using xlim(xaxeslimits);, but the patch function doesn't automatically clip the data to these limits. You need to manually limit the data used in the patch function to the desired x-axis range.

Here's an modified version of your code that addresses this issue by clipping the data arrays x, y, yhbo, yhbr, SEM, SEMhbo, and SEMhbr to the specified x-axis limits:

ROIs = {'VisualROI_R','SensoryROI_R','MotorROI_R','RetrosplenialROI_R',...

'VisualROI_L','SensoryROI_L','MotorROI_L','RetrosplenialROI_L'};

xaxeslimits = [-5 5];

f = figure('InvertHardcopy','off','Color',[1 1 1]);

t = tiledlayout(size(ROIs,2)/2, 2);

x = linspace(-5, 10, 226);

tileind = 2;

for roi = 1:size(ROIs, 2)

nexttile(tileind)

title(ROIs{roi})

eval(['yhbo = means.hbo.' ROIs{roi} ';'])

eval(['SEMhbo = sems.hbo.' ROIs{roi} ';'])

eval(['yhbr = means.hbr.' ROIs{roi} ';'])

eval(['SEMhbr = sems.hbr.' ROIs{roi} ';'])

eval(['y = means.fluo.' ROIs{roi} ';'])

eval(['SEM = sems.fluo.' ROIs{roi} ';'])

y = y-1; % to get centered around 0

%% Modification : Clip data to x-axis limits

idx = x >= xaxeslimits(1) & x <= xaxeslimits(2);

x_clip = x(idx);

y_clip = y(idx);

yhbo_clip = yhbo(idx);

yhbr_clip = yhbr(idx);

SEM_clip = SEM(idx);

SEMhbo_clip = SEMhbo(idx);

SEMhbr_clip = SEMhbr(idx);

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% HbO

yyaxis right

plot(x_clip, yhbo_clip, 'Color', 'red', 'LineWidth', 2)

patch([x_clip, fliplr(x_clip)], [yhbo_clip + SEMhbo_clip fliplr(yhbo_clip - SEMhbo_clip)], 'r' ,'EdgeColor','none', 'FaceAlpha',0.25)

hold on

% HbR

plot(x_clip, yhbr_clip, 'Color', 'blue', 'LineStyle', '-', 'LineWidth', 2)

patch([x_clip, fliplr(x_clip)], [yhbr_clip + SEMhbr_clip fliplr(yhbr_clip - SEMhbr_clip)], 'b' ,'EdgeColor','none', 'FaceAlpha',0.25)

ylim([-1.5 1.5]);

h_label = ylabel('\Delta \muM', 'interpreter', 'Tex', 'Rotation', 270);

ax = gca;

ax.YColor = 'red';

ax.XColor = 'k';

set(ax, 'FontSize', 15, 'LineWidth', 2)

xlim(xaxeslimits);

% Fluo

yyaxis left

plot(x_clip, y_clip, 'Color', [0.4660 0.6740 0.1880], 'LineWidth', 2);

patch([x_clip, fliplr(x_clip)], [y_clip + SEM_clip fliplr(y_clip - SEM_clip)], 'g' ,'EdgeColor','none', 'FaceAlpha',0.25)

f_label = ylabel('\Delta F/F');

ylim([-0.05 0.05]); % centered at 0

ax = gca;

ax.YColor = [0.4660 0.6740 0.1880];

ax.XColor = 'k';

set(ax, 'FontSize', 15, 'LineWidth', 2)

xlabel('Time (sec)')

xlim(xaxeslimits);

if tileind == size(ROIs, 2)

tileind = 1;

else

tileind = tileind+2;

end

end

leg1 = legend({'GCaMP', 'SEM', 'HbO','SEM', 'HbR','SEM'}, 'Orientation', 'Horizontal');

leg1.Location = 'southoutside';

% f.Position = [10 10 1800 1000];

f.Position = [10 10 900 1000];

% save

pause(0.5)

% saveas(gcf, [SaveDir '/NVC/Sham_RS/' Acq '_' type '_' ROIsavename '_AvCurves.tiff'], 'tiff');

% saveas(gcf, [SaveDir '/NVC/Sham_RS/' Acq '_' type '_' ROIsavename '_AvCurves.eps'], 'epsc');

saveas(gcf, "testFig", 'svg');

close(f)

Here is the the screenshot of the SVG file output from this code.

Saving figure as .svg alters appearance (9)

Please refer to the following documentation link to learn more about patch function.

https://www.mathworks.com/help/matlab/ref/patch.html

Hope this helps!

2 Comments

Show NoneHide None

Marleen 1 minute ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180916

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180916

Hi Milan,

Thank you so much! It works!

Adam Danz 1 minute ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180926

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2125876-saving-figure-as-svg-alters-appearance#comment_3180926

This is a viable workaround but it doesn't address the problem. The clipping property on patch objects is set to on by default which is why the line is clipped in the MATLAB figure. There may be a bug in the export that does not honor the clipping in vector format.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsAxis Labels

Find more on Axis Labels in Help Center and File Exchange

Tags

  • image export
  • svg
  • plotting
  • figure export

Products

  • MATLAB

Release

R2023b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Saving figure as .svg alters appearance (12)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Saving figure as .svg alters appearance (2024)

References

Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6099

Rating: 5 / 5 (70 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.