-: Hi guys and welcome back.
In this video, we're going to talk about
very important concepts in matplotlib,
the figure, the axes and the plot.
In the last couple of videos, we've been
using them, but it's not been terribly clear what is what.
So we're gonna make that clear in this video.
Let's get to it.
The figure is the top level container.
Inside a figure, we can include one
or more axes, and inside each axes,
we can have one or more plots.
I'm going to get rid of almost
all this code here, and just leave
the figure and the show, and I'm gonna run this file.
You can see that what we get here
is basically an empty file with nothing inside it.
There's no axes, and there's nothing plotted.
We've used pyplot to create the new figure,
and when we do this, pyplot automatically
selects the figure, and that becomes
the currently active figure.
And when we do plt.show, that is the figure
that we're showing.
Later on, we're going to learn how to assign
these two variables, so that we can show
specific figures when we want to,
but for now, that's how it works behind the scenes.
Every other function in the pyplot library
is going to act on the currently selected figure,
after it is created, until a different one is selected.
Let's now add a set of axes to the figure.
The axes, which is a matplotlib class,
is where plots are drawn.
You can have multiple plots drawn
on a single pair of axes, or you can also
have multiple axes in one figure.
Let's start off by adding a set
of two dimensional axes to our figure.
We can do plt.axes, and now what
this is going to do is create
a pair of axes in the currently selected figure,
which is the one we just created.
Here we can pass in a list of 06020,
for example, and this means that our X axis
is gonna go from zero to six,
and our Y axis is gonna go from zero to 20.
Now if we run this, you'll see that
we get our axes here.
Matplotlib tries to create ticks evenly
throughout the limits, so that we get 2.5,
five, 7.5, and so on, even though we only
defined zero and 20 as our limits.
At the moment though, we've got nothing
drawn in the canvas, so let's change that.
Plots can be drawn on a set of axes,
and each plot needs to specify data points
for the axis that it is drawn on.
So for example, if we have a two dimensional axes,
as we do here, then we need to provide
the X and Y coordinates for each point that we want to draw.
That's why earlier on, when we used plt.plot,
we provided a list of X coordinates,
and a list of Y coordinates,
and these will then map to the axis.
If we run this though, you'll see that
we've got a small problem.
The data points that we've drawn,
one, two, three and four, as well as three,
five, nine and 25, don't exactly fit
within the axes that we've created.
That's why four and 25 gets partially
clipped here going up, because there's not
enough room in our axes for it.
When we created our line chart earlier on,
we didn't use plt.axis to create our axes,
and therefore matplotlib created them for us,
making sure that the plot fit inside
the axes in its entirety.
So you should only use plt.axis when you want
to specifically define your axes limits to certain numbers.
If we run this without that, you'll see
that the plot now fits exactly.
But you can also see that the distribution
of our ticks has changed.
All right, so just to recap,
what we've got here is a figure.
Inside the figure, we can have
one or more sets of axes.
In this case, we have one set of axes.
And inside each axes, we can have one or more plots.
Well in fact, we can have zero or more plots,
if you wanna leave them empty.
Here we've got one plot in our set
of axes, inside our figure.
And as show, we can also have multiple axes,
and they go into rows and columns,
and we can also have multiple plots within each.
Thank you guys for joining me in this video.
I hope you've learned something,
and I'll see you in the next one.