how to save a plot without Margin of figure?

조회 수: 97 (최근 30일)
fred bnm
fred bnm 2018년 5월 23일
댓글: BN 2020년 9월 12일
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에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by