area()图包括绘图轴

时间:2015-06-08 15:06:36

标签: matlab plot

我在MATLAB中创建一个图形,然后着色图形的背景以突出显示区域。这方面的一个例子如下:

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');

在MATLAB中,情节如下所示: MATLAB plot

但生成的pdf文件如下所示: saved pdf

是否有命令将轴线强制回到阴影区域的顶部,就像在MATLAB图中一样?

由于

2 个答案:

答案 0 :(得分:1)

当我运行脚本(R2012b)时,也会在"数字"轴被两个区域掩盖(并且在" .pdf"中)。

似乎问题与绘图有关,而不是转换为" .pdf"。

特别是问题似乎是由于定义的区域尺寸和轴紧的耦合效应"设置。

因此,我已经缩小了区域尺寸,取而代之的是“紧密的”#34;明确定义" xlim"和" ylim"。

另外,我增加了轴线宽度"。

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

希望这有帮助。

答案 1 :(得分:1)

修复很简单,只需要添加

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

强制将轴放在盒子顶部。