MATLAB - dynamically resize x-axis but not y-axis in plots?

Dang Khoa picture Dang Khoa · Sep 9, 2011 · Viewed 10.1k times · Source

I am generating a plot in real-time. I shift the x-axis by 30 seconds, every 30 seconds. This is all well and good, but my y-axis is auto-resizing itself to smaller than previously. Take a look below:

Plot before 30 second threshold - Y limits are [-1 1]

This is my data before we hit the 30 seconds and redraw the x-axis labels. I'm just plotting ±cos(t) right now, so my Y limits are [-1 1].

After 30 second threshold -  Y limits are [-0.8 0.5]

After the 30 seconds, I shift the axes over to start watching the plot generate on the time interval [30 60]. Notice that my Y limits have rescaled to [-0.8 0.5]. As time increases, the limits go back to [-1 1]. But I would like to have continuity between the previous 30 second snapshot and the current snapshot in time, i.e., limits should be [-1 1] immediately after hit the 30 second threshold.

Is there a way to keep the previous Y limits and still let them grow properly (i.e., if Y data goes over limits it'll resize appropriately, automatically)?

Answer

dantswain picture dantswain · Sep 9, 2011

This may not be "automatic" like you're thinking, but I would do something like this.

new_axes = function resize_axes(x_data, y_data, x_increment)

old_axes = axis();
new_axes = old_axes;
if max(x_data(:)) > old_axes(2)
    new_axes(2) = new_axes(2) + x_increment; # e.g., 30 seconds
    new_axes(1) = old_axes(2);  # if you want the new axes to start
                                #  where the old ones ended
end
if max(y_data(:)) > old_axes(4)
    new_axes(4) = max(y_data(:));
end
if min(y_data(:)) < old_axes(3)
    new_axes(3) = min(y_data(:));
end

axis(new_axes);

Then call resize_axes whenever you plot new data.