1

At the moment I'm plotting two points in a grid and I'm using the xlim and ylim to force the scale of my plot, like this

enter image description here

but I'd like to have a plot that changes dynamically the axis depending on where is my target. This because my red point might go above xlim and ylim or way below of it, like these two cases.

enter image description here enter image description here

In the first one, the point is outside the window and therefore I cannot see it, while in the other case, the point is close to the origin of the plot and I'd like to see it more closely, like if I zoom in. For now I'm using

plot(x,y,'.','MarkerSize',20,'Color','r');
xlim([-a a]);
ylim([-a a]);

I think that to do what I need, I should use x,y somehow instead of a or a combination of the two in order to have a more dynamic range depending on the dimension of x,y. Is there any other easier and faster way to do that?

2 Answers 2

3

You can use axis handles and change the XData and YData properties to dynamically change axis limits. Consider the following sample code that plots a random data point and dynamically updates the axis limits,

h = figure;
% get axis handle
ax = gca(h);
set(ax, {'XLim', 'YLim'}, {[-1 1], [-1 1]});

XY = [];

for i = 1:100
    % generate random data point
    xy = 2*randn(1, 2);
    XY = cat(1, XY, xy);

    % get min and max values of points so far
    minVals = min(XY, [], 1);
    maxVals = max(XY, [], 1);

    % plot the data point
    scatter(xy(1), xy(2), 'b*');
    hold on;

    % update the axis limits dynamically
    set(ax, {'XLim', 'YLim'}, {[minVals(1)-1 maxVals(1)+1], [minVals(2)-1 maxVals(2)+1]});
    % pause so that you can see the plot update
    pause(0.5);
end
Sign up to request clarification or add additional context in comments.

Comments

0

I think you're looking for the axis tight command. According to the documentation, what it does is

Fit the axes box tightly around the data by setting the axis limits equal to the range of the data.

...

The limits automatically update to incorporate new data added to the axes. To keep the limits from changing when using hold on, use axis tight manual.

Here's a demonstration:

y = randi(21,10,1)-11;
figure(); hP = plot(NaN(10,1),NaN(10,1),'-o'); axis tight; grid minor;
for ind1 = 1:numel(x)
  hP.XData(ind1) = x(ind1);
  hP.YData(ind1) = y(ind1);
  pause(0.5);
end

Notice how the axis limits change automatically:

Demonstration

2 Comments

Is it also possible to use axis tight + something? Because with axis tight the maximum value in my plot overlaps with the border of my graph
That won't make them tight :) I can't think of a good way to do it, but you can try adding hAx.XLim = hAx.XLim*1.1; hAx.YLim = hAx.YLim*1.1; after the plot (assuming hAx is the axes' handle).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.