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)
0 个评论
回答(2 个)
OCDER
2018-5-23
This might help: https://www.mathworks.com/help/matlab/creating_plots/save-figure-with-minimal-white-space.html
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
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');
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!