Advanced plotting in matlab

In a recent post, I introduced you to the joy of plotting in Matlab. Matlab capabilities in this domain are quite large. Today I want to show you how to arrange figures with multiple plots.

First, let’s start with some basics that were not covered in the previous post.

As we said, to create a graph, you can use the following code :

X=1:100;
Y=X+10;
figure;
plot(X,Y);

figure creates Ā a window and plot the curve, right?

Well not only. In this form, plot also creates automatically an axes object. Figure can store many many different types of objects (buttons for you interface, for instance) and the Axes is one of them.

Axes are zone of the graphic devoted to plots. And axes can display traces but also images,movies, 3d objects, …. In this example, the created axes will be the child of this figure.

Of course, there can be several childs (Matlab is not doing any birth control), so you can have as many Axes as you want in one figure.

Mathworks made a function to do this easily called subplot.

X1=1:100;
Y1=sqrt(X1);
X2=1:100;
Y2=cos(X2);
figure;
subplot(2,1,1);
plot(X1,Y1);
subplot(2,1,2);
plot(X2,Y2);

Subplot(2,1,1) creates one Axes. It behaves as if the entire window was divided in an array that is 2 rows and 1 column. Here it goes in the first of these 2 elements. The figure should be like this :

Similarly, subplot(3,3,6) would divide the figure in a 3 by 3 array and place the Axes in the 6th element. The 6th element is at the 2nd row and the 3rd column.

If you were to ask me, I would say that subplot is a pretty bad name. subplot is not at all a plot function, it is only creating an Axes. This is probably historical. They had to keep the name for backward compatibility.

I don’t really like this last code because it doesn’t keep the link to the actual axes. I like to keep handles available, so I prefer :

X1=1:100;
Y1=sqrt(X1);
X2=1:100;
Y2=cos(X2);
figure;
h1=subplot(2,1,1);
h2=subplot(2,1,2);

plot(h1,X1,Y1);
plot(h2,X2,Y2);

What is nice about this version?

Well first, it is more elegant, as it separates the Axes creation from the plotting part. As a result, it helps you organize your code.

And then, h1 and h2 can be very useful. I personnaly love to do :

linkaxes([h1 h2],'x');

linkaxes is this marvelous function that can connect the Axes limit of several Axes. In this example, it connects the X axe of Axes 1 and 2. So, when you zoom, both plot are adjusted in the same way. Very very handy to look at simultaneous recordings.

Now that you know how to display two curves in two Axes or more, let me ask you the following question :

Who has said when you use subplot that you HAVE to fill in all the elements of the array with a similar Axes?

Not me.

The result of this simple axiom is that you can make awfully complicated plots in a glimpse, like so :

X1=1:100;
Y1=sqrt(X1);
figure;
% 2 by 2 Axes
h(1)=subplot(2,2,4);

% 6 by 4 Axes
h(2)=subplot(6,4,18);
h(3)=subplot(6,4,22);

% 3 by 4 Axes
h(4)=subplot(3,4,1);
h(5)=subplot(3,4,2);
h(6)=subplot(3,4,3);
h(7)=subplot(3,4,4);
h(8)=subplot(3,4,5);
h(9)=subplot(3,4,6);

% 3 by 8 Axes
h(10)=subplot(3,8,17);
h(11)=subplot(3,8,18);

% 6 by 2 Axes
h(12)=subplot(6,2,6);

for i=1:12
   plot(h(i),X1,Y1);
end

Here is the result. I will let you dissect how I did this.

This entry was posted in Intermediate. Bookmark the permalink.

10 Responses to Advanced plotting in matlab

  1. cohen_lhyver says:

    Hi!
    Great post! It really helped me šŸ™‚
    Just a little thing on the last code: the ‘h’ variable is not declared so an error message is displayed –> “??? Assignment between unlike types is not allowed.”
    Cheers!

  2. Jerome says:

    I suppose using “clear h;” before the code will help cleaning this variable if you have used it previously.

  3. Yep! That’s it! šŸ™‚

  4. Sam says:

    I think there is a TYPO here:
    “Similarly, subplot(3,3,6) would divide the figure in a 3 by 3 array and place the Axes in the 5th element. Remember that Matlab is column-major so the 6th element is at the 2nd row and the 3rd column.”

    Actually it should be:
    “Similarly, subplot(3,3,6) would divide the figure in a 3 by 3 array and place the Axes in the 6th element. Remember that Matlab is column-major so the 6th element is at the 3rd row and the 2nd column.”

    i.e. “6th element” and “3rd row and 2nd column”

    Regards,

    P.S.: Thanks for this knowledge database.

  5. shahzaib abbasi says:

    very helpful for learners

  6. Shprk says:

    Thanks it’s really helpful. I was wondering how I can fix both axes x and y?

  7. Muhammad Asif says:

    Excellent, tremendously helpful for me

Leave a Reply

Your email address will not be published. Required fields are marked *