Loren on the Art of MATLAB

April 30th, 2010

Coordinating Views

Ever wanted to have coordinated views of multiples plots when you pan or zoom? You can do this in MATLAB.

Let's suppose I have two plots and I want to be sure they keep their x-axes synchronized. I'll start by loading the sunspot data.

sdata = load('sunspot.dat');
time = sdata(:,1);
spots = sdata(:,2);

I've heard that there is an 11-year cycle for sunspots. So let me generate a sinusoid with a period of 11 years so we can then do a visual comparison (vs. the more appropriate Fourier analysis!).

cycle11 = sin(time*pi*2/11);

Now let's plot the two curves, but in different axes since the scalings are quite different and I don't want to worry about them.

hspots = subplot(2,1,1);
plot(time,spots)
h11 = subplot(2,1,2);
plot(time,cycle11)

Here I ensure the plots have the same x-axis by explicitly plotting the data using the same x-coordinates. But let's say I want to zoom in now. I can achieve this by setting both x-axes with the same limits. I could do them one at a time, or together.

set([hspots h11],'xlim',[1900 1950]);

I can still make "mistakes" though, by setting the limits on only one of the axes.

set(h11,'xlim',[1850 1900])

I can achieve the synchronization, with a little less mental effort than carrying around all affected axes, by using the function |linkaxes.

linkaxes([h11 hspots],'x')
set(h11,'xlim',[1800 1850])

Do you have a simple way to coordinate your plots? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.10

2 Responses to “Coordinating Views”

  1. Jonas replied on :

    To coordinate plots, I use linzoom from the file exchange: http://www.mathworks.com/matlabcentral/fileexchange/21414

  2. James Ross replied on :

    This is a great capability and I have used it and extended it to multiple figure windows.

    I have created a GUI that reads data in multiple formats and provides push-button plotting support. I link axes across multiple figures, allowing the user to zoom in to the same time frame on all signals plotted using the GUI. Zoom in on any figure, then select a menu item and all figures are updated to use that same time scale.

    The plotting routine also computes statistical and frequency information on each data signal. Updating the axes recomputes this information using only the data from the current time scale. This has proven very valuable in quickly isolating and analyzing issues that occur for brief time periods within a larger data set.


MathWorks
Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.