how to save a plot without Margin of figure?

92 Ansichten (letzte 30 Tage)
fred bnm
fred bnm am 23 Mai 2018
Kommentiert: BN am 12 Sep. 2020
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)

Antworten (2)

OCDER
OCDER am 23 Mai 2018
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 Kommentare
OCDER
OCDER am 23 Mai 2018
Bearbeitet: OCDER am 23 Mai 2018
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 am 12 Sep. 2020
Thank you @OCDER, It solved my problem.

Melden Sie sich an, um zu kommentieren.


mohsen Kondori
mohsen Kondori am 15 Jan. 2020
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

Community Treasure Hunt

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

Start Hunting!

Translated by