Saving figure as .svg alters appearance
13 ビュー (過去 30 日間)
古いコメントを表示
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1709991/image.png)
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:![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1709996/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1709996/image.png)
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
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, [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 件のコメント
Stephen23
2024 年 6 月 6 日
編集済み: Stephen23
2024 年 6 月 6 日
Note that using EVAL to access structure fields has been deprecated for around twenty years:
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})
Checking:
isequal(yhbo,yhbo_better)
採用された回答
Milan Bansal
2024 年 6 月 6 日
編集済み: Milan Bansal
2024 年 6 月 6 日
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1710126/image.png)
Please refer to the following documentation link to learn more about patch function.
Hope this helps!
2 件のコメント
Adam Danz
2024 年 6 月 6 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Blue についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!