MATLAB is not giving good quality eps figure, looks good in png format but looks really bad in pdf.

I have attached my MATLAB figure MATLAB figure (gdrive), Rendered with Painters. I normally use "saveas" function, to save figure in eps format, to be exact:
saveas(fig, 'myfigure1.eps', 'epsc');
When I use generated eps in overleaf (latex pdf) it looks very bad (seems aliased may be). See the pic below. The peculier red contours are visible. In upper blue region some weird lines like structures appears. In png it looks fine.
Actully it looks better in MATLAB figure, also looks fine in png format. See pic below. I want in eps format.
I used many functions in file exchange but couldn't find a solution. I also ovserved that if I use print command to create pdf directly, even with 6000 dpi the figure looks bad.
Can I have eps of this MATLAB figure with high quality?

Antworten (1)

For article/document/presentation images I suggest you to use export_fig from FEX.
The images have a better quality and more saving options (e.g. no white borders).
filepath = 'C:\Users\YourName\Desktop\filename';
export_fig(filepath,'-eps','-transparent','-r300');

14 Kommentare

Thanks a lot, but it is not working, already tried, as I mentioned I tried other few functions also. I have provided the MATLAB figure, even you can check.
This never occured to me so it's hard to tell without looking at the figure data.
Have you tried to save directly in .pdf with export_fig?
For figure data you can click on "MATLAB figure (gdrive)", I have provided that in my question's 1st line also.
For me both .eps and .pdf work fine.
Did you download ghostscript and xpdreader?
I don't see your problem even if I use Matlab's saveas.
Nice, it looks good in your overleaf, can you give me the steps you followed to save the MATLAB figure in eps as well as pdf format ? (I want no background or white background which is grey in your overleaf)
I did not understand what do you mean by "Did you download ghostscript and xpdreader?"
FYI, I am using CentOS 7.
ghostscript and xpdftool are external tools used by export_fig.
You can use one of these three to remove the background:
fig = open('MatlabQuestion.fig');
% Method 1:
export_fig('myfigure1', '-eps', '-transparent');
% Method 2:
exportgraphics(fig, 'myfigure2.eps', 'BackgroundColor', 'None', 'ContentType', 'Vector')
% Method 3:
ax = findall(fig, 'Type', 'Axes');
set(ax, 'Color', 'None');
saveas(fig, 'myfigure3.eps', 'epsc');
I suggest you to use Method 1 because from my tests is the only one that does not create aliasing lines.
You can find many discussions about this topic on the forum [1] [2] [3] where different solutions are proposed. However I suggest you to use export_fig.
Ok I installed ghostscript.x86_64 and xpdf.x86_64 in my CentOS 7. Then I downloaded export_fig code, used following commands:
fig = open('MatlabQuestion.fig');
export_fig('myfigure1', '-eps', '-transparent');
Good thing is the weird lines are not there. It created black background figure. See below. What to do ?
Weird. It has been a while I did not use the transparent option, but i don't remember this behaviour.
You can use the same approach from previous answers:
fig = open('MatlabQuestion.fig');
set(gca, 'Color', 'None');
export_fig('myfigure1', '-eps');
It gives figure as below in overleaf. Can it have no background or white background?
My bad I changing the axes rather than teh figure;
You can use this:
set(fig, 'Color', 'w');
I think the black background come from the fact that;
set(fig, 'Color', 'None');
Sets the background to black.
Well after all these efforts, I don't think it is creating right eps image.
As you can see images as below, (a) is default save, (b) is generated by "painters" (c ) is very high resolution image (used 'r1500')
You can clearly see that (b) is vector image, (a) and c) are not vecctor image. You can see the text below each figure, "(a)" or "(b)" or "( c)" looks really crisp, but the text on the figure generated looks really bad in (a) and (c ). The (b) seems vector image but as you can see the colours are weird. Can there be solution ?
See above, text (a) looks great, but figure is not vector figure, hence text is really bad.
In above image, it seems vector image, so everything looks great, except those weird colours.
Even very high resolution image seea above looks really bad.
t's probably connected to the use of the contour function, but this is my guess.
If this is for a paper, .eps figures are in general preferable, but you can also use .pdf or high resolution .png.
Alternatively, you can convert the contour to a surface.
I don't recommend you doing this because produces a large amount of data and heavy figures which makes overleaf copilation much slower, but should work in desprate cases.
close all;
% Input:
resolutionFactor = 10;
% Load data:
fig = open('MatlabQuestion.fig');
ax = fig.Children(3);
cont = findall(ax.Children, 'Type', 'Contour');
% Re-structure:
for j = 1 : length(cont)
cont2surf(ax, cont(j), resolutionFactor);
end
% - [stacking goes here] - %
% Export figure:
export_fig('myfigureR.eps', '-eps', '-r300');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cont2surf(ax, cont, resolutionFactor)
% Resolution:
xCurrentResolution = length(cont.XData);
yCurrentResolution = length(cont.YData);
xNewResolution = resolutionFactor * xCurrentResolution;
yNewResolution = resolutionFactor * yCurrentResolution;
% function cont2surf(ax, x, y, z)
% Increase data resolution:
xPoints = linspace(1, xCurrentResolution, xNewResolution);
yPoints = linspace(1, yCurrentResolution, yNewResolution);
xInterp = @(n) interp1(1 : xCurrentResolution, cont.XData, n);
yInterp = @(n) interp1(1 : yCurrentResolution, cont.YData, n);
[X,Y] = meshgrid(xInterp(xPoints), yInterp(yPoints));
[x,y] = meshgrid(cont.XData,cont.YData);
Z = interp2(x, y, cont.ZData, X, Y);
% Substitute contour with surface:
delete(cont);
hold on;
surf(X, Y, Z, 'Parent', ax, 'EdgeColor', 'None');
hold off;
view([0 0 1]);
ax.Children = ax.Children(circshift(1 : length(ax.Children), -1));
ax.SortMethod = 'ChildOrder';
end
Remember to change the stack order to display the graphic object as you want.
the stack is accessed through ax.Children and you can change the order as I have done in the function:
%% Example:
% Indeces for new order:
newOrder = randperm(length(ax.Children));
% Setup new order:
ax.Children = ax.Children(newOrder);
% Update stack based on Children order:
ax.SortMethod = 'ChildOrder';
I think without -painters it is not giving correct vector image.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023a

Gefragt:

am 14 Jun. 2023

Kommentiert:

am 20 Jun. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by