フィルターのクリア

how to save a plot without Margin of figure?

64 ビュー (過去 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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by