Print vectorized graphic of patches without edges

I need to export a vectorized graphic of material data from a finite element simulation. I don't want the lines of the mesh to show, I only want the faces (whose colors correspond to a colormap of material data values). Currently, 'patch' is working well to create exactly the image I need in the MatLab figure. However, I've noticed that when I print to pdf, the image is still pixelated. I am already using the '-painters' renderer. Any tips on how to fix this would be greatly appreciated.
Attached is 'patch_plotter.m' demonstrating my issue, along with a few other things to help narrow down the issue.
In patch_plotter, ax(1,1) shows the image that I'm interested in creating a vectorized version of. Also, ax(1,3) shows that when I allow the edges to be visible, the pdf image indeed does have vectorized angled edges as desired (but like I said, I don't want the edges to be showing, and the edges also result in sloppy spikes sticking out at their endpoints).
Is it possible that the actual faces of the patches are not vectorized graphics while the edges of the patches are vectorized graphics? ( ax(2,3) strongly suggests this conclusion. )

4 Comments

It is not clear, what exactly your problem is. I do see this in your PDF:
But what is the problem? What do you see in "ax(1,3)" called "vectorized angled edges as desired"? ax(2,2) looks confusing.
i haven't looked at your data, but in general...
  • i have noticed that setting alpha properties makes exporting vector art trickier
  • pdf is just a container and doesn't specify that contents need to be vector vs rastered, maybe try some other exclusively vector content file types?
Interesting, thanks for your comment. I'm fine not using transparency for this particular task.
As for trying other file types, I just now tried .svg and .eps; both appear to have the same issue. Thanks for the suggestion though.
@Jan, ax(1,1) is the image that I'm trying to create, except that if you zoom in to the pdf, you can see that the angled edges are not vectorized, they are pixelated, which is not desired. You may have to zoom in to the pdf using Adobe Acrobat Reader (or something other than a browser tab; I can't zoom in far enough with chrome to see the pixelation).
All of the other axes are just documentation of the steps I've already taken to debug the issue:
  1. ax(1,2) shows that if I set LineStyle = 'none' instead of EdgeColor = 'none', I have the same pixelation issue.
  2. ax(1,3) shows that if I allow edges to be visible, the edges do not appear pixelated. This is what I mean by "vectorized angled edges as desired". However, I want to get rid of the edges.
  3. ax(1,4) shows that if I use EdgeColor = 'none', and I set FaceColor = 'flat' instead of FaceColor = 'interp', I have the same pixelation issue.
  4. ax(2,1) shows that if I use LineStyle = 'none', and I set FaceColor = 'flat' instead of FaceColor = 'interp', I have the same pixelation issue.
  5. ax(2,2) shows that if I assign colors to the faces instead of the vertices, I have the same pixelation issue.
  6. ax(2,3) is highly informative, and shows that if I set the LineWidth = 1e-6 (for the edges), then I (again) have vectorized edges, but pixelated faces. This suggests to me that MatLab only exports the edges of patch graphics as vectorized graphics, but exports the faces as pixelated graphics. This probably works well for people who want the edges to be visible and thick, since the vectorized edges will cover up the messy pixelation of the faces. But when the edges aren't there, or are very thin, the pixelated faces are apparent. See the image below, which is a zoomed portion of ax(2,3).

Sign in to comment.

Answers (2)

I now believe it is because you are technically plotting 3D patches (telling Matlab there's a z-coordinate, even if they are all zero)
Extract just the x and y coordinates
patch('Faces',dat.t' + 1,'Vertices',dat.p(1:2,:)','FaceVertexCData',dat.d1','FaceColor','flat');
and a quick test looks like pdf export without any tweaks will output vector

8 Comments

Good thought, but it actually doesn't fix the issue. The image you are showing in test.pdf has edges. I am suspecting that matlab exports the edges in vectorized graphics, but exports the faces in pixelated graphics. My ultimate goal is to have a vectorized image of only the faces (without the edges showing). Unfortunately, the results I see after eliminating the z-coordinates are still pixelated.
oh I see...I picked the wrong example to fixate on :)
So i made another observation. I randomly color each face and make FaceColor = 'flat' so we can see more clearly what's going on (like your (2,2) image), and only plotted a fraction of the patches.
load('dat.mat')
T = dat.t' + 1;
V = dat.p(1:2,:)';
C = rand(height(V),3);
figure;
p = patch('Faces',T(1:150,:),...
'Vertices',V,...
'FaceVertexCData',C,...
'FaceColor','flat',...
'EdgeColor','none');
daspect([1 1 1])
set(gca,'Visible','off')
print(gcf,"test.pdf","-dpdf")
Well, the online image above doesn't actually show it, but the attached pdf does...if you look at face edges that don't touch any other faces they look like vector images. But the edges between adjacent faces (faces that share 2 points) look pixelated, not because the edges are, but because the coloration seems to be. If you look at faces that share only at 1 point, sometimes the color along the edges are partially pixelated.
I'm wondering if this is just an issue with the instructions for coloration encoded in the pdf, or ps, or however it works.
Jan
Jan on 8 Aug 2021
Edited: Jan on 8 Aug 2021
@J. Alex Lee: I do see it in your PDF using Acrobat Reader:
The resolution of of the line between colored and white is fine, the line between the colored is rought. And some pixels are completely orphaned?!
When I run your code under Matlab 2018b, Win10, I get:
I have disables "smooth vector graphics" in Acrobat reader. With enabling it, the orange/white line has no artifacts anymore:
So this is either a problem of your graphics driver or of the Matlab version.
Oh interesting...I ran these all on a mac os 10.15.7 on a pretty old intel. Matlab version is 2020b. I looked at the pdf using the Apple Preview app...
I am not sure what you meaned by orphaned pixels, but in the first image the other colors in the purple, that is what i observed even when there are only 2 triangular faces sharing 1 vertex, which i thought was particularly interesting.
Jan
Jan on 9 Aug 2021
Edited: Jan on 9 Aug 2021
@J. Alex Lee: Yes, I've called these pixels "orphaned":
Did you install all updates of Matlab? Are there newer drivers for the graphics card?
The problems in your PDFs are not caused by the PDF viewer.
I'm seeing the same result as @J. Alex Lee, where interfaces between two patches are pixelated, but edges of a patch that are not shared with another patch are vectorized. I'm also seeing the orphaned pixels when I run J Alex Lee's code on my machine. I'm running MatLab R2021a Windows 10.
Temporarily ignoring the issue of orphaned pixels, I wrote a code that at least highlights the first issue - where vectorization of graphics depends on whether a patch has a differently colored patch adjacent to it. @Jan, if your graphics are coming out cleanly with MatLab R2018b Windows 10, maybe I should try that. Also, @Jan, when you run the code below, do you see vectorization or do you see pixelation in ax(1) and ax(2)?
clear; close all;
f = figure();
t = tiledlayout(3,1);
for j = 1:3
ax(j) = nexttile;
daspect([1 1 1])
% set(ax(j),'Visible','off')
end
j = 1;
axes(ax(j))
p(j) = patch('Faces',[1 2 3; 3 1 4],'Vertices',[0 0; 0 1; 2 1; 2 0],'FaceVertexCData',[0 0 0; 1 1 1],'LineStyle','none','FaceColor','flat');
title(ax(j),'black patch next to white patch (no edges)')
j = 2;
axes(ax(j))
p(j) = patch('Faces',[1 2 3; 3 1 4],'Vertices',[0 0; 0 1; 2 1; 2 0],'FaceVertexCData',[0 0 0; 1 1 1],'LineWidth',1e-6,'FaceColor','flat');
title(ax(j),'black patch next to white patch (with edges)')
j = 3;
axes(ax(j))
p(j) = patch('Faces',[1 2 3],'Vertices',[0 0; 0 1; 2 1],'FaceVertexCData',[0 0 0],'LineStyle','none','FaceColor','flat');
title(ax(j),'black patch (no white patch) (no edges)')
print(f,'mypdf_sol','-painters','-dpdf','-bestfit')
@Alex Ogren: I have to modify the code a little bit, because tiledlayout is not available in R2018b:
f = figure();
for j = 1:3
ax(j) = subplot(1,3,j);
daspect([1 1 1])
end
...
Then I get a smooth PDF file (open the image in a new tab to see its full size):
The other 2 diagrams look equivalenty.
It is time to send the question to MathWorks support team. The orphaned colored pixels are clearly a bug.
I also just tried on R2018b Windows 10, and the patches are smooth and vectorized. Thank you both for your help in getting to the bottom of this!
I'm not sure who to give answer credit to since @J. Alex Lee discovered that adjacent patches are buggy, and @Jan discovered that they work in R2018b - am I allowed to accept two answers?

Sign in to comment.

Using exportgraphics might do a little better.
exportgraphics(f,'mypdf2.pdf','ContentType','Vector')
Looks better to me, although better on some of your patches than others. Cool images!

1 Comment

Hmm, yes, thanks Dave. It looks like the issue is less pronounced when I use exportgraphics. Here are the results I'm observing when zooming in on the same region of ax(1,1) after generating the pdf with 'print' vs. 'exportgraphics'. (I'm still looking for a true vectorized graphics solution though).
print:
exportgraphics:

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 5 Aug 2021

Commented:

on 9 Aug 2021

Community Treasure Hunt

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

Start Hunting!