altmany / export_fig

A MATLAB toolbox for exporting publication quality figures
BSD 3-Clause "New" or "Revised" License
1.28k stars 368 forks source link

1) export_fig(filename,'-transparent') not working, 2) Line width of markers is reduced #335

Closed nathanpli closed 3 years ago

nathanpli commented 3 years ago

I downloaded export_fig from https://www.mathworks.com/matlabcentral/fileexchange/23629-export_fig/. I was trying to use the most basic options, but ran into issues. See this example code.

export_fig version 3.14 MATLAB version 2019b update 8 64-bit mac, Big Sur OS v 11.1

x = 0:10;
y = 0:10;
plot(x,2*y,'-r','LineWidth',6);
hold on
plot(x,y/4,'-k');
plot(x,y,'rx','MarkerSize',16,'LineWidth',10);
plot(x,y/2,'kx','MarkerSize',16,'LineWidth',1);
ax = gca;
ax.YLim = [0 10];
ax.Color = 'none';
export_fig('example1.png', '-transparent')
export_fig('example2.png')
export_fig('example3.png', '-r600', '-transparent')
saveas(gcf, 'example4.png', 'png');

I followed the example in the readme for exporting images with transparent backgrounds, but the image failed to export properly. (See example1.png). Calling export_fig('example2.png') exported a low resolution image. I used the debugger and found that options.resolution was being set to 864 in line 1588 in the parse_args function when no resolution was specified as an input argument, so I am confused about why the image quality is so low. Specifying the resolution -r600 improved the image quality; however, the line widths of the markers were reduced to 1. See example3.png and compare with example4.png using the saveas() function. The line widths of lines are not reduced, but the markers look thinner in the exported png's.

After this, I quit MATLAB and restarted it, but that did not fix the issues.

example1.png (appears blank) example1

example2.png example2

example3.png example3

example4.png example4

altmany commented 3 years ago

The first problem (apparently empty file) is the same as issue #318

The second problem (narrow markers) is a direct result of the way that the builtin Matlab print command works when using a non-default resolution (-r600 in your case, which is 6.25 times the standard Windows resolution of 96). You can try this for example:

print(gcf, '-r600', 'ex5', '-dpng'); 
winopen ex5.png

(in other words, the narrow markers using -r600 is the expected behavior, not a bug)

I encourage you to retry your 4 examples using the latest commit.