You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Saving figure as a image
24 views (last 30 days)
Show older comments
I am saving my output figures as images. But when I run my matlab script from different computers, the figures look different. As in the labels and legends are different sizes, etc. I want the image outputs to be exactly the same no matter what computer I run it on, I do not want to have computer specific scripts.
I am not a graphics person, so I do not know what causes this. Whether it is the screen resolution, renderer, file type, options or what? I do not know where to start.
The code I am using to save figure is:
output_file = 'example_image.png'
saveas(figure1,fullfile(folderName, output_file));
which outputs the following image:

Help!!
9 Comments
meghannmarie
on 12 Apr 2021
I just tried using exportgraphics and the output graphics are still different (see attached). I saved the images using the following code:
exportgraphics(figure1,output_file_fig,'Resolution',300)
Are there any more settings for exportgraphics I should be specifying? Or maybe something when I am creating the figure or axes?
meghannmarie
on 13 Apr 2021
Attach this?
Adam Danz
on 13 Apr 2021
No, I'm asking about the figure file (.fig). If you save the figure and attach it we can more easily try some options with exportgraphics or other functions. After saving the fig, open it and make sure it appears correctly. Also, what Matlab release are you using?
meghannmarie
on 13 Apr 2021
Edited: meghannmarie
on 13 Apr 2021
Okay, I attached the figure made on hpc and same figure made on my laptop and the images made. When I open the figures made on hpc on my laptop the top x axis looks messed up, but it looks right in output image made on hpc. I cannot look at fig on hpc because only command prompt.
I was using 2019a on laptop and hpc, but then switched to 2021a on laptop and 2020b on hpc (newest version avail) to try to use exportgraphics function.
Downloading 2020b now on laptop to see if that fixes fig file that was made on hpc.
meghannmarie
on 13 Apr 2021
Edited: meghannmarie
on 13 Apr 2021
What I am trying to do is get graphics that look exactly the same no matter what computer they are run on. I am running the same scripts (and data) on our high performance computer and my local laptop and getting graphics that look different. I do not want to have to tweak graphics depending on what computer they are run on (it can be 100s of graphics). I need them to be the same because then they are emdedded into another document.
I do not need to save as pdf, that was just the other persons suggestions, I would prefer an image file.
If you look at .png image outputs from each computer, they look different. The axes are labeled differently.
I am thinking it is something to do with the size of figure window/fonts and how it is choosing what labels to show or not to show that is making it different...
Accepted Answer
Adam Danz
on 13 Apr 2021
Comparing the two png files in one of your previous comments shows an upper y axis in one figure that doesn't exist in the second figure. This is more than a version difference. The only why that would be possible is if the two systems are using different versions of the code or if the system using the older Matlab release is quitting early due to an error.
There is also a difference in fontsize. That can be fixed by explicitly setting the fontsizes in your code. The difference in figure size can be controlled by explicitly setting figure size.

10 Comments
meghannmarie
on 13 Apr 2021
Edited: meghannmarie
on 13 Apr 2021
Which font size settting, I am comparing the properties and it all looks to be set the same?
Adam Danz
on 13 Apr 2021
Edited: Adam Danz
on 13 Apr 2021
I would start with
- fig.Position (where fig is your figure handle)
- ax.FontSize (where ax is your axis handle)
- ax.Position
But more importantly, clearly there is a 2nd axis in the image on the right (in my answer) with green tick labels at the top along the x axis.
The grid line styles also appear to differ.
meghannmarie
on 13 Apr 2021
I do not understand why the second axis looks like this in the fig file, but not in the png file that was made on the computer that created the fig file!
I have now downloaded 2020b on my laptop, trying to adjust the settings you talked about then re-run everything and compare using the same versions.
One difference is my laptop is a windows machine where the hpc is linux machine.
Adam Danz
on 13 Apr 2021
I believe the problem can be addressed so the fig appears the same on all of the machines and matlab releases you mentioned.
It could be something as basic as display size differences and not correctly setting and linking axis positions.
Adding a second axis layer must be done with care and very offen results in these types of problems when done incorrectly.
You should be using linkprop to link the axes positions so that if one axis position changes, the other will change to. I'd bet with a fair amount of confidence that one of your axes is being resized when the legend is added and the other axis is not linked so it doesn't resize. That explains why the 2nd axis doesn't align with the first.
That doesn't explain when the 2nd axis doesn't appear at all in the figure on the left in my answer. It's either completely underneath the top axis or isn't there at all.
meghannmarie
on 13 Apr 2021
OK, I set the figure property for Position and axes properties for FontSize and FontSizeMode. Am I setting the linkprop correctly in code below, I have never used that before?
figure1 = figure('Position',[100,100,550,450],'Visible','off');
% Create axes
axes2 = axes('Parent',figure1,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'XColor',[56/255 87/255 35/255],...
'YColor',[56/255 87/255 35/255],...
'Color','None',...
'FontSizeMode','manual',...
'FontSize',10);
%... more code
ax2_pos = axes2.Position; % position of first axes
axes1 = axes('Position',ax2_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'XColor',[35/255 65/255 115/255],...
'YColor',[35/255 65/255 115/255],...
'Color','none',...
'FontSizeMode','manual',...
'FontSize',10);
hlink = linkprop([axes1,axes2],{'Position'});
meghannmarie
on 13 Apr 2021
Okay, here are my new sample fig files and graphics files. When I open up fig file made on hpc on my laptop the title is shifted off of figure area. I do not know why, it is not doing that on the hpc (only when I open the fig back on laptop) - you can see that in the output PNG file, it looks correct. Both figures are made with the same code in 2020b, only difference is the hpc version is a linux machine and I am compiling code versus laptop being windows and I am just running function in matlab. I think both are working, the figure is just translating wrong bringing it back to my local machine.
Comparing the output PNG and JPG files, they look very close, but output image files are different sizes. For example: PNG made on HPC is 1146x938x24 whereas the PNG made on laptop is 859x703x24 and JPG made on HPC is 1562x1318x24 whereas the JPG made on laptop is 1208x1002x24.
This is code I am using to save PNG:
saveas(figure1,fullfile(folderName,output_file))
This is the code I am using to save JPG:
exportgraphics(figure1,output_file,'Resolution',220)
I think this is the major difference to overcome still. How to save out fig files so the output graphics files are the same (or very close to it). Any ideas on different ways to save files or options to set so the resulting file is the same size/resolution?
Adam Danz
on 13 Apr 2021
To fix the title position problem, assign the title to the first axes (axes2). Also, for demo purposes, avoid using Visible-off with figures unless visibility is the focus.
figure1 = figure('Position',[100,100,550,450]);
% Create axes
axes2 = axes('Parent',figure1,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'XColor',[56/255 87/255 35/255],...
'YColor',[56/255 87/255 35/255],...
'Color','None',...
'FontSizeMode','manual',...
'FontSize',10);
%... more code
ax2_pos = axes2.Position; % position of first axes
axes1 = axes('Position',ax2_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'XColor',[35/255 65/255 115/255],...
'YColor',[35/255 65/255 115/255],...
'Color','none',...
'FontSizeMode','manual',...
'FontSize',10);
hlink = linkprop([axes1,axes2],{'Position'});
title(axes2, sprintf('Density vs Depth\nJanuary'))

Adam Danz
on 13 Apr 2021
Regarding the inconsistent figure sizes, there are many areas to troubleshoot and I've found that exportgraphics does some unexpected things with figure size. For example in this example from the documentation, the printed fig should be "approximately 3 inches" but it's much larger when i try it (r2021a).
The export_fig function on the file exchange will likely work for you:
meghannmarie
on 16 Apr 2021
The export_fig fuunction still did not work. I ended up putting in a tech support ticket. There were a few differences in the computers that were causing the differences. One computer was windows and other computer was linux where I was using command prompt to run scripts. On the different computers the renderers were different, the screen resolutions were different, and they had different fonts. Once I compensated for all this (plus the other things you helped me with), then the graphics turned out very similar. Only differences left are slight differences in fonts and file size which both are just because its a different OS.
I attached the final graphics and here is my final code:
% Set Renderer
opengl software
set(groot, 'DefaultFigureRenderer', 'painters');
% Create figure
figure1 = figure('Position',[100,100,550,450],...
'PaperPositionMode','auto',...
'InvertHardcopy','on',...
'Visible','off');
% Create axes
axes2 = axes('Parent',figure1,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'XColor',[56/255 87/255 35/255],...
'YColor',[56/255 87/255 35/255],...
'Color','None',...
'FontSizeMode','manual',...
'FontName','Arial',...
'FontSize',10);
hold(axes2,'on');
axis(axes2,'ij');
title(axes2,{[parameter ' vs. Depth' ],month_long{n}})
% ..more code here
% Create plot
ax2_pos = axes2.Position; % position of first axes
axes1 = axes('Position',ax2_pos,...
'XAxisLocation','bottom',...
'YAxisLocation','left',...
'XColor',[35/255 65/255 115/255],...
'YColor',[35/255 65/255 115/255],...
'Color','none',...
'FontSizeMode','manual',...
'FontName','Arial',...
'FontSize',10);
linkprop([axes1,axes2],{'Position'});
% ..more code here
% output resolution and format
outputDPI = '-r900';
outputFmt = '-dpng';
% control computer (hpc) screen pixel's per inch
dispPPI = 72;
% current computer's screen pixel's per inch
ppi = get(0, 'screenpixelsperinch');
% compensate for different DPI
scale = ppi / dispPPI;
figure1.Position(3:4) = figure1.Position(3:4)*scale;
name = fullfile(folderName,output_file);
print(figure1, name, outputFmt, outputDPI);
close(figure1);
Thanks for all your help!!
More Answers (1)
David Hill
on 1 Apr 2021
You might try figure2pdf in file exchange.
2 Comments
meghannmarie
on 1 Apr 2021
It seems like this function is just outputting file to a pdf, which I need a graphic file. I will try it when I have access to my second computer, but do you know what in theis code makes the graphics the same?
Adam Danz
on 13 Apr 2021
meghannmarie's answer moved here as a comment
-----------------------------------------------------------------------------
The figure2pdf function does not work. The pictures still save differently on different machines.
Look at my attachments, one is saved on my laptop and one is saved on our hpc. They are different, but same exact script. I think it is something to do with resolution of screen, but how do I get around it?
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)