防止数据接触轴

问题描述 投票:0回答:3

在 MATLAB 中,是否可以快速/简洁地增加图中数据周围的默认填充?换句话说,我不希望数据离轴太近。

enter image description here

matlab plot matlab-figure
3个回答
3
投票

如果简单地填充现有轴就足够了,那么以下应该可以工作。假设您想每边添加 10%。

plot(...);
scale = 1.1;
ax = axis();
xc = 0.5 * (ax(1)+ax(2));
yc = 0.5 * (ax(3)+ax(4));
c = [xc,xc,yc,yc];
axis(scale * (ax - c) + c);

2
投票

这种方法类似于@jodag的优秀answer,并且完全是我的偏好。根据评论中OP的要求发布此内容。我毫不怀疑可能有更有效的方法来做到这一点。

关键思想: 自动使用

xlim
ylim

最小工作示例:

d = 0.10;     % 10 percent
c = [1-d 1+d];
X = 5 + 5*rand(10,2);

plot(X(:,1),X(:,2),'rs')

Xrng = xlim;
Yrng = ylim;
xlim(c.*Xrng);  % Adjust X Range
ylim(c.*Yrng);  % Adjust Y Range

使用这些属性实现自动化的其他方法:

  1. 使用绘图手柄
    h = plot()
    并修改属性
  2. 使用
    set
    get
    gca
    命令。

毫无疑问还有其他方法。


0
投票

对于足够新的 Matlab 版本,您可以在绘图后使用

axis padded
命令。

© www.soinside.com 2019 - 2024. All rights reserved.