-: Hi guys, and welcome back.
In this video we're going to learn
about the object-oriented approach
when working with matplotlib.
Up until now, we've been using the functional approach
where we use the plt import to basically do everything
and everything that we do acts on the currently
active figure or pair of axes.
However, using the object-oriented approach,
we can start to assign things to variables,
and we can use them interchangeably
in our application whenever we want.
So we're going to learn about that in this video.
Let's get started.
When you're developing larger matplotlib applications,
rather than just small scripts and using it interactively,
the object-oriented approach can help
make your code more explicit.
So let's rewrite the simple line chart that we've got here
using the object-oriented approach.
The first thing to do is to create our figure,
and this selects it so that other functions
within the plt library will act upon it.
But it's better practise, especially as I said,
with larger applications,
to start assigning things to variables
so that you can use them instead of relying
on the plt library to do things for you.
So here we're creating the figure.
And now we've got this figure variable,
and we can use it to create a pair of axes.
So we'll do figure.add subplot.
Note that figure.add subplot does add the axes
to the figure, but it also gives you the axes
for you to work with.
So now when we want to plot something
like this line graph here, we can use axes.plot,
and that goes into this axes.
At the end, we do plt.show, and that is going to display
the figure that has these axes where we've plotted
our line chart.
So let me run this and you'll see what comes out
is exactly what we had before.
Note that we're still using plt.show.
You can normally do figure.show,
but this will only work
depending on which backhand you're using.
Figure.show is great for for example,
showing in Jupyter Notebooks
and interactive Python interpreters and things like that,
but plt.show will actually handle creating that window,
let you interact with it, and so on.
So for the most part, I recommend using plt.show,
especially now that we are constantly running these files
and we want to get that window to pop up.
Almost everything that we'll do
with the object-oriented approach,
which is the approach that we're gonna follow
in this section, you can do with
the function-based approach as well.
However, I prefer the object-oriented approach
because I feel it produces cleaner code.
We can set the title for each pair of axes,
and we can do something like axes.set_title,
and we can pass it in here.
For example, that.
Note that here we're interacting directly with the axes
that we want to set the title for,
and we're not relying on the plt library
on having this specific pair of axes currently selected.
We can also do things like set x label to set the labels
and axes.set y label to set the y label axes.
Here I'm just using test data,
so it doesn't make too much sense
but this is what you end up with.
We've modified this pair of axes specifically
and that is what this code achieves.
So that's everything for this video,
I just wanted to show you how it is different
to use the object-oriented approach
and how you'll end up interacting
with these specific variables and objects
rather than relying on the plt library.
I hope you've enjoyed this video,
thank you for joining me and I'll see you in the next one.