Print vectorized graphic of patches without edges
Show older comments
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
Jan
on 6 Aug 2021
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.
J. Alex Lee
on 6 Aug 2021
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?
Alex Ogren
on 6 Aug 2021
Alex Ogren
on 7 Aug 2021
Answers (2)
J. Alex Lee
on 7 Aug 2021
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
Alex Ogren
on 7 Aug 2021
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.
@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.
J. Alex Lee
on 8 Aug 2021
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.
@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.
Alex Ogren
on 9 Aug 2021
@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.
Alex Ogren
on 9 Aug 2021
Dave B
on 6 Aug 2021
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
Alex Ogren
on 6 Aug 2021
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!



