I am making a 3D surface plot in MATLAB, and I want to rasterize the surface plot (to save image size and loading speed) while keeping the rest of the figure (e.g., axes, labels, titles, etc.) as vector graphics. How can I do this? Right now I have the following:
x = linspace(-2, 2, 201);
y = linspace(-2, 2, 201);
[X, Y] = meshgrid(x, y);
Z = sin(sqrt(X.^2 + Y.^2));
fig = figure;
colormap parula
surf(X, Y, Z, 'FaceColor', 'interp', 'EdgeColor', 'none', 'FaceLighting', 'gouraud')
daspect([4 4 1])
axis tight
view(-50, 30)
camlight left
exportgraphics(fig, 'test_matlab_02.pdf', 'ContentType', 'vector')
If I set ContentType
to vector
, then the whole figure is vector graphics. If I set ContentType
to image
, then the whole figure becomes rasterized. How can I have a rasterized surface plot with vector graphics for the remaining features in the figure?
File Exchange offers vecrast, which might work in this case. It’s supposed to be able to combine rasterized and vector formats.
From the description of the vecrast
function:
vecrast is a function that allows to automatically save a figure with mixed vector and raster content. More specifically, two copies of the figure of interest are created, rasterFigure and vectorFigure. Patches, surfaces, contours, images, and lights are kept in rasterFigure but removed from vectorFigure. rasterFigure is then saved as a temporary .png image with the required resolution. The .png file is subsequently inserted into the vectorFigure, and the result is saved in a single vector file.
The function is able to handle both 2D and 3D plots, as specified in the notes:
- The graphics smoothing (anti-aliasing) is turned off for the raster figure. This improves sharpness at the borders of the image and at the same time greatly reduces file size. You may change this option in the script by setting 'GraphicsSmoothing', 'on' (line 97).
- A resolution of no less than 300 dpi is advised. This ensures that interpolation at the edges of the raster image does not cause the image to bleed outside the prescribed axis (make a test with 20dpi on the first example and you will see what I mean).
- The stacking option has been introduced to accomodate 2D and 3D plots which require the image behind or in front the axis, respectively. This difference can be seen in the examples below.
- I strongly advise to take a look at the tightPlots function that allows setting exact sizes of figures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With