-: Hi guys, and welcome back.
In this video, we're going to learn how to add
multiple axes to one figure, so that we can
end up exporting images that have multiple graphs in them.
Let's get started.
Here we've got some of the code from the last video.
We've got our figure that we're creating,
and then we're adding a subplot, which is the axes,
then in there, we're plotting our line chart.
What we can also do though is we can rename
this to, let's say, AX1 for axis one and axis two,
and we're gonna do add subplot again.
Now we do have two subplots in our figure,
and we can grab that and plot something again.
So I'm gonna plot, in this case, a slightly
different set of numbers, one, two, three,
four in the X axis, but five, seven,
eleven, and seventeen in the Y axis.
Now, if we run this, you might expect,
since you've already seen it,
and I've already mentioned, that you'll have
two sets of axes in your figure,
but that's actually not what's going to happen at all.
Here we have one pair of axes, and two plots in it.
So what's happened?
Well by default, when we do figure.add subplot,
matplotlib is going to add the subplot
in the top left hand corner of our figure.
Figures are essentially divided into cells.
The top left hand corner is cell number one,
the next one to its right is cell number two,
the next one to its right is cell number three,
and so on, and it goes left to right, top to bottom,
like the diagram on the screen is currently showing.
But here we've not told matplotlib to create
any number of rows and columns.
We've only told it to create different subplots.
So by default, they're both the same subplot.
If we want to specify different subplots,
we can use the nomenclature of number of rows,
comma, number of columns, comma, the index for this axes.
So where this axes is going to appear within that grid.
Here we're defining that this axis
will be the top left hand corner one,
in a grid of two columns.
So this one is gonna be one, two, two,
to be the one at the top right,
in a grid of two columns.
Now if we do this, we can run this again,
and you see that we get two different graphs.
Now the lines are also the same colour,
because matplotlib is aware that these
are two separate plots in two different places.
The user is not gonna get confused
between which one is which.
When working with matplotlib, you'll often see this.
One two one, one two two, just like that.
It means exactly the same thing,
but matplotlib allows us to pass
in a single set of numbers, as apposed
to three comma separated values.
I really don't like this at all,
and I would encourage you not to use it,
but it is something that you're gonna see
every now and then, when working with matplotlib code.
And obviously it doesn't work when the number
of cells reaches a number greater than nine,
because then you need four digits instead of three.
So generally I would recommend using
the three number approach, with three
different arguments, rather than passing in a single number.
Here we have created a figure and two axes,
and we can do this using this piece of code.
However, there is a shorthand that we can use
when working with multiple subplots.
Instead of three different variables there,
we can do figure, X1, X2, equal plt.subplots,
and that is going to create one figure
with two subplots, in this same structure.
AX1 is gonna be the first one.
AX2 is gonna be the second one.
Note that I'm getting these errors here,
because the variables are re-declared,
but if we get rid of those, that goes away.
If you start wanting to have multiple
rows and columns, like for example two and two,
then you're gonna start running into some problems,
because AX1 and AX2 will now start meaning each row.
So AX1 will be the first row, with two plots.
X2 will be the second row, with two plots.
So instead what we have to do is something like this.
So here we have AX1 and AX2, and then in a separate
tuple, AX3 and AX4.
If you're not familiar with tuple,
the structuring by the way, I've got
a resource linked in the resources section
of this lecture, for you to check out.
What's happening here is that plt.subplots
is returning a figure, and a tuple of tuples,
and we are then de-structuring that
tuple of tuples into four different variables.
Again, if we go back to one two,
and we do AX1 and AX2, subplots
is now returning a tuple with figure
and two axes, and we're de-structuring
those two into their two variables.
This works in exactly the same way.
All right, that's everything for this video.
Thank you guys for joining me on this one.
I hope you've learned something,
and you'll see you in the next one.