WEBVTT

1
00:00:00.400 --> 00:00:01.720
<v ->Hi guys, and welcome back.</v>

2
00:00:01.720 --> 00:00:03.020
In this video we're going to learn

3
00:00:03.020 --> 00:00:05.000
about the object-oriented approach

4
00:00:05.000 --> 00:00:06.920
when working with matplotlib.

5
00:00:06.920 --> 00:00:09.120
Up until now, we've been using the functional approach

6
00:00:09.120 --> 00:00:13.020
where we use the plt import to basically do everything

7
00:00:13.020 --> 00:00:15.620
and everything that we do acts on the currently

8
00:00:15.620 --> 00:00:18.370
active figure or pair of axes.

9
00:00:18.370 --> 00:00:20.160
However, using the object-oriented approach,

10
00:00:20.160 --> 00:00:22.190
we can start to assign things to variables,

11
00:00:22.190 --> 00:00:24.020
and we can use them interchangeably

12
00:00:24.020 --> 00:00:26.080
in our application whenever we want.

13
00:00:26.080 --> 00:00:27.900
So we're going to learn about that in this video.

14
00:00:27.900 --> 00:00:28.800
Let's get started.

15
00:00:29.700 --> 00:00:32.210
When you're developing larger matplotlib applications,

16
00:00:32.210 --> 00:00:35.810
rather than just small scripts and using it interactively,

17
00:00:35.810 --> 00:00:37.310
the object-oriented approach can help

18
00:00:37.310 --> 00:00:39.190
make your code more explicit.

19
00:00:39.190 --> 00:00:42.320
So let's rewrite the simple line chart that we've got here

20
00:00:42.320 --> 00:00:44.610
using the object-oriented approach.

21
00:00:44.610 --> 00:00:47.510
The first thing to do is to create our figure,

22
00:00:47.510 --> 00:00:50.010
and this selects it so that other functions

23
00:00:50.010 --> 00:00:53.240
within the plt library will act upon it.

24
00:00:53.240 --> 00:00:55.740
But it's better practise, especially as I said,

25
00:00:55.740 --> 00:00:57.630
with larger applications,

26
00:00:57.630 --> 00:00:59.350
to start assigning things to variables

27
00:00:59.350 --> 00:01:01.900
so that you can use them instead of relying

28
00:01:01.900 --> 00:01:04.710
on the plt library to do things for you.

29
00:01:04.710 --> 00:01:07.250
So here we're creating the figure.

30
00:01:07.250 --> 00:01:09.530
And now we've got this figure variable,

31
00:01:09.530 --> 00:01:12.310
and we can use it to create a pair of axes.

32
00:01:12.310 --> 00:01:14.963
So we'll do figure.add subplot.

33
00:01:16.370 --> 00:01:20.870
Note that figure.add subplot does add the axes

34
00:01:20.870 --> 00:01:23.260
to the figure, but it also gives you the axes

35
00:01:23.260 --> 00:01:24.710
for you to work with.

36
00:01:24.710 --> 00:01:26.990
So now when we want to plot something

37
00:01:26.990 --> 00:01:30.800
like this line graph here, we can use axes.plot,

38
00:01:30.800 --> 00:01:33.470
and that goes into this axes.

39
00:01:33.470 --> 00:01:36.680
At the end, we do plt.show, and that is going to display

40
00:01:36.680 --> 00:01:39.100
the figure that has these axes where we've plotted

41
00:01:39.100 --> 00:01:40.932
our line chart.

42
00:01:40.932 --> 00:01:42.900
So let me run this and you'll see what comes out

43
00:01:42.900 --> 00:01:44.653
is exactly what we had before.

44
00:01:45.660 --> 00:01:48.720
Note that we're still using plt.show.

45
00:01:48.720 --> 00:01:51.690
You can normally do figure.show,

46
00:01:51.690 --> 00:01:53.430
but this will only work

47
00:01:53.430 --> 00:01:56.760
depending on which backhand you're using.

48
00:01:56.760 --> 00:01:58.960
Figure.show is great for for example,

49
00:01:58.960 --> 00:02:00.730
showing in Jupyter Notebooks

50
00:02:00.730 --> 00:02:03.530
and interactive Python interpreters and things like that,

51
00:02:03.530 --> 00:02:07.310
but plt.show will actually handle creating that window,

52
00:02:07.310 --> 00:02:09.440
let you interact with it, and so on.

53
00:02:09.440 --> 00:02:12.000
So for the most part, I recommend using plt.show,

54
00:02:12.000 --> 00:02:14.330
especially now that we are constantly running these files

55
00:02:14.330 --> 00:02:16.310
and we want to get that window to pop up.

56
00:02:16.310 --> 00:02:18.164
Almost everything that we'll do

57
00:02:18.164 --> 00:02:18.997
with the object-oriented approach,

58
00:02:18.997 --> 00:02:20.230
which is the approach that we're gonna follow

59
00:02:20.230 --> 00:02:21.930
in this section, you can do with

60
00:02:21.930 --> 00:02:23.790
the function-based approach as well.

61
00:02:23.790 --> 00:02:25.740
However, I prefer the object-oriented approach

62
00:02:25.740 --> 00:02:28.290
because I feel it produces cleaner code.

63
00:02:28.290 --> 00:02:30.890
We can set the title for each pair of axes,

64
00:02:30.890 --> 00:02:34.040
and we can do something like axes.set_title,

65
00:02:34.040 --> 00:02:35.720
and we can pass it in here.

66
00:02:35.720 --> 00:02:37.930
For example, that.

67
00:02:37.930 --> 00:02:41.110
Note that here we're interacting directly with the axes

68
00:02:41.110 --> 00:02:43.040
that we want to set the title for,

69
00:02:43.040 --> 00:02:45.440
and we're not relying on the plt library

70
00:02:45.440 --> 00:02:49.000
on having this specific pair of axes currently selected.

71
00:02:49.000 --> 00:02:54.000
We can also do things like set x label to set the labels

72
00:02:54.740 --> 00:02:59.503
and axes.set y label to set the y label axes.

73
00:03:00.530 --> 00:03:01.700
Here I'm just using test data,

74
00:03:01.700 --> 00:03:03.270
so it doesn't make too much sense

75
00:03:03.270 --> 00:03:04.830
but this is what you end up with.

76
00:03:04.830 --> 00:03:07.650
We've modified this pair of axes specifically

77
00:03:07.650 --> 00:03:09.460
and that is what this code achieves.

78
00:03:09.460 --> 00:03:10.470
So that's everything for this video,

79
00:03:10.470 --> 00:03:12.950
I just wanted to show you how it is different

80
00:03:12.950 --> 00:03:14.670
to use the object-oriented approach

81
00:03:14.670 --> 00:03:16.460
and how you'll end up interacting

82
00:03:16.460 --> 00:03:18.800
with these specific variables and objects

83
00:03:18.800 --> 00:03:20.940
rather than relying on the plt library.

84
00:03:20.940 --> 00:03:21.990
I hope you've enjoyed this video,

85
00:03:21.990 --> 00:03:24.890
thank you for joining me and I'll see you in the next one.

