Area()

I am creating a graph in MATLAB and then shading the background of the graph to highlight areas. An example of this is the following:

clc; clear all;

hFig = figure;
y = [0:0.1:2*pi];
x = sin(y);
plot(y,x);
hold on
h(1) = area([0 (2*pi)/2], [1 1],-1);
set(h(1),'FaceColor',[1.0 0.8 0.6],'EdgeColor',[1.0 0.8 0.6]);
h(2) = area([(2*pi)/2 2*pi], [1 1],-1);
set(h(2),'FaceColor',[1.0 0.5 0.5],'EdgeColor',[1.0 0.5 0.5]);
axis tight

set(gca,'children',flipud(get(gca,'children')));

%# centimeters units
X = 14.0;                  %# paper size
Y = 12.0;                  %# paper size
xMargin = 1;               %# left/right margins from page borders
yMargin = 1;               %# bottom/top margins from page borders
xSize = X - 2*xMargin;     %# figure size on paper (widht & hieght)
ySize = Y - 2*yMargin;     %# figure size on paper (widht & hieght)

set(hFig, 'PaperUnits','centimeters')
set(hFig, 'PaperSize',[X Y])
set(hFig, 'PaperPosition',[xMargin yMargin xSize ySize])
set(hFig, 'PaperOrientation','portrait')

print('example','-dpdf','-r0');

      

In MATLAB, the plot looks like this: MATLAB plot

But the generated PDF looks like this: saved pdf

Is there a command to force the centerlines backward over the shaded areas, as it does in MATLAB plot?

thank

+3


source to share


2 answers


The fix was simple, just needed to add

set(gca,'layer','top');

      



to force the axis to be inserted into the square.

+1


source


When I run the script (R2012b), also in the "figure" the axis is masked with two regions (and also in the ".pdf").

The problem seems to be with the plotting, not the conversion to .pdf.

In particular, the problem arises from the effect of linking the size of certain areas and setting the "rigid axis".

So, I reduced the size of the areas, replaced "tight on the axis" by explicitly defining "xlim" and "ylim".

I have also increased the line width axis.



clc; clear all;

hFig = figure;
y = [0:0.1:2*pi];
x = sin(y);
plot(y,x);
hold on
% Modified area extend
% h(1) = area([0 (2*pi)/2], [1 1],-1);
h(1) = area([0.02 (2*pi)/2], [.99 .99],-.995);
set(h(1),'FaceColor',[1.0 0.8 0.6],'EdgeColor',[1.0 0.8 0.6]);

% Modified area extend
% h(2) = area([(2*pi)/2 2*pi], [1 1],-1);
h(2) = area([(2*pi)/2 2*pi-.01], [.99 .99],-.995);
set(h(2),'FaceColor',[1.0 0.5 0.5],'EdgeColor',[1.0 0.5 0.5]);

% Replaced "axis tight with explicit "xlim" and "ylim"
% axis tight
set(gca,'xlim',[0 2*pi],'ylim',[-1 1])
% Increased axis "linewidth" (notr strictly necessary
set(gca,'linewidth',1)
set(gca,'children',flipud(get(gca,'children')));

%# centimeters units
X = 14.0;                  %# paper size
Y = 12.0;                  %# paper size
xMargin = 1;               %# left/right margins from page borders
yMargin = 1;               %# bottom/top margins from page borders
xSize = X - 2*xMargin;     %# figure size on paper (widht & hieght)
ySize = Y - 2*yMargin;     %# figure size on paper (widht & hieght)

set(hFig, 'PaperUnits','centimeters')
set(hFig, 'PaperSize',[X Y])
set(hFig, 'PaperPosition',[xMargin yMargin xSize ySize])
set(hFig, 'PaperOrientation','portrait')

print('example','-dpdf','-r0');

      

enter image description here

enter image description here

Hope it helps.

+1


source







All Articles