- A value of 'current' sets the background color to the parent container's color.
- A value of 'none' sets the background color to transparent or white, depending on the file format and the value of ContentType:
- Transparent — For files with ContentType='vector'
- White — For image files, or when ContentType='image'
- When ContentType='auto', MATLAB sets the background color according to the heuristic it uses to determine the type content to save.
- Alternatively, specify a custom color or a named color.
How do I stop exportgraphics from adding a border
16 views (last 30 days)
Show older comments
Hi Everyone
I'm using Matlab 2024b and have found an oddity.
If I load an image and then export it again exportgraphics seem to add a white border
In its simplest form...
fig = figure();
im = imread('peppers.png');
imshow(im)
exportgraphics(gca,'exportplot.jpg');
If you compare peppers.png with the exported version then you can see a pixel wide border has been added

I've tried
set(gca, 'color', 'none');
axis off
exportgraphics(gca,'plot2.jpg','BackgroundColor','none')
but none of these seem to make any difference. What's annoying is that I'm also using exportgraphics to generate animated gifs but the white margin appears around those too. This really shows up on the black background that I'm using.
Any suggestions on how to eliminate this?
0 Comments
Answers (1)
dpb
about 20 hours ago
Edited: dpb
about 16 hours ago
That would be expected behavior...
Background color, specified as 'current', 'none', an RGB triplet, a hexadecimal color code, or a color name. The background color controls the color of the margin that surrounds the axes or chart.
Try
exportgraphics(gca,'plot2.jpg','BackgroundColor','auto')
and see if it's smart enough to match background closely enough to not be objectionable. If not you may have to set a specific RGB color.
It doesn't appear there's a way to not have a bounding box of some color.
2 Comments
dpb
22 minutes ago
I would suggest you submit the request for a 'Box' named parameter analogous to that for the axes properties to Mathworks as an official enhancement request at <Product Support Page>.
As for the tabs; there has been at least one other thread of a similar nature with the same conclusion; at the moment there is no control available to achieve the desired visual effect and that desires/requirements be submitted as enhancement request. Such user input does at least influence future developments but without that input there will not be any real impetus for changes.
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
