altmany / export_fig

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

Using patch(X,Y,C) with export_fig #346

Closed dio-medes closed 2 years ago

dio-medes commented 2 years ago

I am trying to plot a parch(x,y,c) polygon on a line plot. I have a matrix called data oraginsed like the following:

period | line | upperbound | lowerbound
0      | 0.02 | 0.04      | -0.01  
1      | 0.04 | 0.06      | 0.03
.     .
.     .
.     .

The relevant matlab code looks like

fig1 = figure('Units', 'centimeters', ...
    'Position', [1 1 16.99 12]);
hold on;

% Plot the shaded area
shaded_region = patch([transpose(data(:,1)) fliplr(transpose(data(:,1)))], ...
 [transpose(data(:,3)) fliplr(transpose(data(:,4)))], 'red');

% Plot the line
line(data(:,1),data(:,2));

% Export
export_fig fig1 -pdf -transparent

The results are a very pixelated PDF. I suspect this is a problem with the way patch interacts with export_fig, as it also takes a relatively long time for this code to run compared to other figures.

Is this a bug or am I doing something wrong?

Note: reposted from mathworks discussion section.

altmany commented 2 years ago

export_fig uses Matlab's builtin print function. In some situations (such as yours) where print finds it difficult to vectorize the image, it incorporates a raster image in the PDF, instead of a vectorized representation. Exporting to SVG format, or using exportgraphics(hFigure,filename,'ContentType','vector') uses different mechanisms, both of which result in vectorized output.