binaryfunt
binaryfunt

Reputation: 7137

Prevent data from touching axes

In MATLAB, is it possible to quickly/concisely increase the default padding around data in plots? In other words, I don't want the data to be too close to the axes.

enter image description here

Upvotes: 2

Views: 387

Answers (3)

Florian D
Florian D

Reputation: 71

For a Matlab version recent enough, you can use the axis padded command after your plots.

Upvotes: 1

SecretAgentMan
SecretAgentMan

Reputation: 2854

This approach is similar to @jodag's excellent answer and is entirely my preference. Posting this based on OP's request in the comments. I have no doubt there may be more efficient ways of doing this.

Key Idea: Automate use of xlim and ylim.

Minimal working example:

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

Other ways to automate this using these properties:

  1. Use a handle for the plot h = plot() and modify properties
  2. Use the set, get, and gca commands.

No doubt there are other approaches still.

Upvotes: 2

jodag
jodag

Reputation: 22314

If simply padding the existing axis is sufficient then the following should work. Say you want to add 10% to each side.

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

Upvotes: 3

Related Questions