区域()绘图覆盖绘图轴

3

我正在使用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图中一样?
谢谢。

我可以在R2015b上重现这个问题。 - Luis Mendo
2个回答

1
这段文本的英译中文为:

修复很简单,只需要添加即可。

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

将轴强制置于框之上。

1
当我运行脚本(R2012b)时,“figure”中的轴也被两个区域遮挡了(在“.pdf”中也是如此)。
似乎问题与绘图有关,而不是转换为“.pdf”。
特别是,问题似乎是由定义区域大小和“axis tight”设置的耦合效应引起的。
因此,我稍微减小了区域的大小,用“xlim”和“ylim”的显式定义替换了“axis tight”。
另外,我增加了轴的“linewidth”。
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

希望这可以帮到您。

嗨il_raffa,感谢您的回答。我没有想到只需将框稍微缩小一点就可以与轴对齐。这肯定有效,但实际上我已经找到了解决方法,之前只是搜索了错误的术语。 - Steve Hatcher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接