gpt4 book ai didi

matlab - area() 绘图覆盖绘图轴

转载 作者:太空宇宙 更新时间:2023-11-03 20:34:05 25 4
gpt4 key购买 nike

我在 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 图中一样?

谢谢

最佳答案

当我运行脚本 (R2012b) 时,“图形”中的轴也被两个区域遮盖(在“.pdf”中也是如此)。

问题似乎与绘图有关,而不是与“.pdf”的转换有关。

特别是问题似乎是由于定义的区域大小和“轴紧”设置的耦合效应造成的。

所以我略微减小了区域大小,用“xlim”和“ylim”的明确定义替换了“axis tight”。

另外,我增加了轴“线宽”。

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

希望这对您有所帮助。

关于matlab - area() 绘图覆盖绘图轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com