Matlab 图为 png
Matlab figure as png
我正在将 matlab 图保存为 png。我想将打印尺寸设置为紧贴屏幕尺寸,所以我使用了:
set(gca(),'LooseInset',get(gca(),'TightInset')); %set print size tight to the screen size
但是,我的图像没有按照屏幕大小保存,也没有保存在屏幕中央...
这是我已经尝试过的所有代码:
function []=FilledCircle1(x0,y0,Radius,N,col1)
if(N<=1)
error('N must be greater than 1');
end
hold on
axis equal
% axis off
hold on
t=(0:N)*2*pi/N; %t=-pi:0.01:pi
x=Radius*cos(t)+x0;
y=Radius*sin(t)+y0;
c1=fill(x,y,col1);
set (c1, 'edgecolor','k')
set(gcf,'PaperUnits','inches','PaperSize',[0.8666,0.8666],'PaperPosition',[0 0 0.8666 0.8666])%setting size (130/150, 130/150, 150pixels per inch being the default size of img), paper position is imporrtant as otherwise i will have extra border
% % set(gcf,'Position', [0 0 4.4466 3.5733], 'units','inches')
% % iptsetpref('ImshowBorder','tight');
set(gca(),'LooseInset',get(gca(),'TightInset')); %set print size tight to the screen size
% set(gcf, 'Position', get(0,'screensize'));
set(gcf,'color','none'); %set backgroound color to transparent
fig = gcf;
fig.InvertHardcopy = 'off'; %saves the fig with the set background color
我希望我的 png 文件看起来像这样:
然而它看起来像这样:
有人可以帮助我了解我做错了什么吗?
谢谢!
这个问题是因为 x 和 y 刻度的标签取代了你的图表。
与其搞乱 TightInset
,我只想设置 axes
的 Position
属性 以使轴的内部占据整个图形
hfig = figure();
hax = axes();
t = (0:N)*2*pi/N; %t=-pi:0.01:pi
x = Radius*cos(t)+x0;
y = Radius*sin(t)+y0;
c1 = fill(x,y,col1);
set(c1, 'edgecolor','k')
set(hfig, 'PaperUnits', 'inches', ...
'PaperSize', [0.8666,0.8666], ...
'PaperPosition', [0 0 0.8666 0.8666], ...
'InvertHardCopy', 'off')
axis(hax, 'equal')
axis(hax, 'off')
set(hax, 'Position', [0 0 1 1]);
set(hfig, 'Color', 'none');
print(hfig, '-dpng', 'output.png')
您还应该考虑使用 export_fig
,因为它可以更忠实地再现屏幕上显示的图形。
我正在将 matlab 图保存为 png。我想将打印尺寸设置为紧贴屏幕尺寸,所以我使用了:
set(gca(),'LooseInset',get(gca(),'TightInset')); %set print size tight to the screen size
但是,我的图像没有按照屏幕大小保存,也没有保存在屏幕中央...
这是我已经尝试过的所有代码:
function []=FilledCircle1(x0,y0,Radius,N,col1)
if(N<=1)
error('N must be greater than 1');
end
hold on
axis equal
% axis off
hold on
t=(0:N)*2*pi/N; %t=-pi:0.01:pi
x=Radius*cos(t)+x0;
y=Radius*sin(t)+y0;
c1=fill(x,y,col1);
set (c1, 'edgecolor','k')
set(gcf,'PaperUnits','inches','PaperSize',[0.8666,0.8666],'PaperPosition',[0 0 0.8666 0.8666])%setting size (130/150, 130/150, 150pixels per inch being the default size of img), paper position is imporrtant as otherwise i will have extra border
% % set(gcf,'Position', [0 0 4.4466 3.5733], 'units','inches')
% % iptsetpref('ImshowBorder','tight');
set(gca(),'LooseInset',get(gca(),'TightInset')); %set print size tight to the screen size
% set(gcf, 'Position', get(0,'screensize'));
set(gcf,'color','none'); %set backgroound color to transparent
fig = gcf;
fig.InvertHardcopy = 'off'; %saves the fig with the set background color
我希望我的 png 文件看起来像这样:
然而它看起来像这样:
有人可以帮助我了解我做错了什么吗? 谢谢!
这个问题是因为 x 和 y 刻度的标签取代了你的图表。
与其搞乱 TightInset
,我只想设置 axes
的 Position
属性 以使轴的内部占据整个图形
hfig = figure();
hax = axes();
t = (0:N)*2*pi/N; %t=-pi:0.01:pi
x = Radius*cos(t)+x0;
y = Radius*sin(t)+y0;
c1 = fill(x,y,col1);
set(c1, 'edgecolor','k')
set(hfig, 'PaperUnits', 'inches', ...
'PaperSize', [0.8666,0.8666], ...
'PaperPosition', [0 0 0.8666 0.8666], ...
'InvertHardCopy', 'off')
axis(hax, 'equal')
axis(hax, 'off')
set(hax, 'Position', [0 0 1 1]);
set(hfig, 'Color', 'none');
print(hfig, '-dpng', 'output.png')
您还应该考虑使用 export_fig
,因为它可以更忠实地再现屏幕上显示的图形。