how to save a plot without Margin of figure?

39 次查看(过去 30 天)
after using plot, i need save as figure. but saveas function incorporate margin of figure. how to save a plot without Margin of figure?
clc; clear;
mask = zeros(400,600);
mask = logical(mask);
position = [300,200];
degree = 45;
L = 50;
x1 = position(1)+ (L*cosd(degree)) ;
y1 = position(2)+ (L*sind(degree)) ;
figure,
imshow(mask)
hold on
plot([position(1) x1],[position(2) y1],'w','LineWidth',8);
%save plot in the form of image and display that
saveas(gca,'mask22.jpg');
img = imread('mask22.jpg');
figure;
imshow(img)

回答(2 个)

OCDER
OCDER 2018-5-23
Modify the code after you plot:
plot([position(1) x1],[position(2) y1],'w','LineWidth',8);
set(gca, 'units', 'normalized'); %Just making sure it's normalized
Tight = get(gca, 'TightInset'); %Gives you the bording spacing between plot box and any axis labels
%[Left Bottom Right Top] spacing
NewPos = [Tight(1) Tight(2) 1-Tight(1)-Tight(3) 1-Tight(2)-Tight(4)]; %New plot position [X Y W H]
set(gca, 'Position', NewPos);
saveas(gca,'mask22.jpg');
  3 个评论
OCDER
OCDER 2018-5-23
编辑:OCDER 2018-5-23
Looks like imshow has its own definition of TightInset, which seems like a bug... Here's a workaround:
mask = zeros(400,600,'logical'); %<==You can do logical zeros here
position = [300,200];
degree = 45;
L = 50;
x1 = position(1)+ (L*cosd(degree)) ;
y1 = position(2)+ (L*sind(degree)) ;
figure
Hx = imshow(mask);
set(gca, 'DataAspectRatioMode', 'auto') %<== Need this to prevent unpredictable behavior, BUT you loose the aspect ratio of the original image!
hold on
plot([position(1) x1],[position(2) y1],'w','LineWidth',8);
hold off
set(gca, 'unit', 'normalize')
set(gca, 'position', [0 0 1 1]);
saveas(gca,'mask22.jpg');
BN
BN 2020-9-12
Thank you @OCDER, It solved my problem.

请先登录,再进行评论。


mohsen Kondori
mohsen Kondori 2020-1-15
Hi,
I have this error when use position in above codes. ERROR= (Undefined function 'position' for input arguments of type 'double'.)
how I can use it??
Thank you

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by