WEBVTT

1
00:00:00.210 --> 00:00:01.460
<v ->Hi guys and welcome back.</v>

2
00:00:01.460 --> 00:00:03.170
In this video, we're going to start learning

3
00:00:03.170 --> 00:00:04.870
about matplotlib and we're going

4
00:00:04.870 --> 00:00:06.990
to draw a simple line chart.

5
00:00:06.990 --> 00:00:08.123
So let's get started.

6
00:00:09.130 --> 00:00:11.030
To draw any graph or chart,

7
00:00:11.030 --> 00:00:12.890
we first need to create a figure.

8
00:00:12.890 --> 00:00:15.520
And inside a figure, we can draw graphs or charts.

9
00:00:15.520 --> 00:00:19.050
In matplotlib, these graphs or charts are called plots.

10
00:00:19.050 --> 00:00:21.600
And plots are drawn on top of axes,

11
00:00:21.600 --> 00:00:23.040
the plural for axis.

12
00:00:23.040 --> 00:00:25.810
Most plots are drawn on x- and y-axes,

13
00:00:25.810 --> 00:00:27.000
although some plots can be drawn

14
00:00:27.000 --> 00:00:29.020
on three-dimensional axes as well.

15
00:00:29.020 --> 00:00:30.950
We're gonna start by importing pyplot.

16
00:00:30.950 --> 00:00:34.940
So I'll do import matplotlib.pylot as plt.

17
00:00:34.940 --> 00:00:38.300
This is the standard import for matplotlib.

18
00:00:38.300 --> 00:00:42.490
Then to create a figure, we just do plt.figure.

19
00:00:42.490 --> 00:00:43.780
You can see that PyCharm is complaining

20
00:00:43.780 --> 00:00:45.830
that I have an unvalid import here

21
00:00:45.830 --> 00:00:48.000
because I haven't installed matplotlib yet,

22
00:00:48.000 --> 00:00:49.290
so let's do that.

23
00:00:49.290 --> 00:00:51.830
You can do this over in your terminal.

24
00:00:51.830 --> 00:00:54.620
Make sure that you have activated your virtual environment

25
00:00:54.620 --> 00:00:57.483
and then simply do pip instal matplotlib.

26
00:00:58.460 --> 00:01:01.780
And if this fails, you should upgrade pip first.

27
00:01:01.780 --> 00:01:03.060
But here you can see it passes

28
00:01:03.060 --> 00:01:05.610
and matplotlib is now installed.

29
00:01:05.610 --> 00:01:08.260
So the red line goes away.

30
00:01:08.260 --> 00:01:09.610
Now, after creating a figure,

31
00:01:09.610 --> 00:01:12.300
we can go ahead and plot inside that figure.

32
00:01:12.300 --> 00:01:16.480
So we can do plt.plot, one, two, three, four, for example,

33
00:01:16.480 --> 00:01:20.100
and three, five, nine, and 25.

34
00:01:20.100 --> 00:01:21.560
Now, running this, which I'm gonna do

35
00:01:21.560 --> 00:01:23.790
by right clicking and pressing on run

36
00:01:23.790 --> 00:01:25.180
is not going to do anything

37
00:01:25.180 --> 00:01:26.670
because we haven't told matplotlib

38
00:01:26.670 --> 00:01:28.960
to display the output produced.

39
00:01:28.960 --> 00:01:31.900
We can do that with plt.show.

40
00:01:31.900 --> 00:01:33.320
Now we can run this file again.

41
00:01:33.320 --> 00:01:37.070
I'm gonna click the green play icon at the top right

42
00:01:37.070 --> 00:01:38.350
and you can see that we get

43
00:01:38.350 --> 00:01:41.900
our matplotlib graph viewer here.

44
00:01:41.900 --> 00:01:43.440
A lot of this is not stuff

45
00:01:43.440 --> 00:01:44.820
that we've developed ourselves.

46
00:01:44.820 --> 00:01:47.530
We've just told matplotlib to draw this graph

47
00:01:47.530 --> 00:01:48.363
but it comes with matplotlib

48
00:01:48.363 --> 00:01:50.140
and it allows you to do things

49
00:01:50.140 --> 00:01:52.470
like pan around the axes, for example,

50
00:01:52.470 --> 00:01:53.560
and zoom in and out

51
00:01:53.560 --> 00:01:56.010
and do some configuration as well.

52
00:01:56.010 --> 00:01:59.460
Plt.plot is used to plot any arbitrary data.

53
00:01:59.460 --> 00:02:02.200
Is creates a 2D set of axes,

54
00:02:02.200 --> 00:02:04.300
and by default, it produces a line graph

55
00:02:04.300 --> 00:02:06.810
where these points are the x-coordinates

56
00:02:06.810 --> 00:02:09.310
and these points are the y-coordinates.

57
00:02:09.310 --> 00:02:12.170
And then it joins the points with a straight line.

58
00:02:12.170 --> 00:02:13.870
We can change it so that instead of joining the point

59
00:02:13.870 --> 00:02:14.810
with a straight line,

60
00:02:14.810 --> 00:02:17.480
it uses circles for the points let's say.

61
00:02:17.480 --> 00:02:19.260
We can do that with a third argument.

62
00:02:19.260 --> 00:02:21.530
I'm putting in the string o.

63
00:02:21.530 --> 00:02:24.180
If we do this, you'll see that we get circles now

64
00:02:24.180 --> 00:02:26.820
instead of a joined line.

65
00:02:26.820 --> 00:02:29.020
There's also a list of built-in colours

66
00:02:29.020 --> 00:02:31.410
that we can use to colour the circles or lines

67
00:02:31.410 --> 00:02:33.470
or anything else automatically.

68
00:02:33.470 --> 00:02:35.300
Here I'm using ko as the string

69
00:02:35.300 --> 00:02:37.083
to colour these circles black.

70
00:02:38.010 --> 00:02:40.050
K is the letter for the colour black

71
00:02:40.050 --> 00:02:41.820
because B is for blue.

72
00:02:41.820 --> 00:02:43.020
We can also get rid of that

73
00:02:43.020 --> 00:02:47.750
and assign the result of plt.plot to a variable,

74
00:02:47.750 --> 00:02:49.650
and that gives us the plot itself.

75
00:02:49.650 --> 00:02:52.300
The line or the points that we've drawn.

76
00:02:52.300 --> 00:02:54.650
Then we can do plt.setp

77
00:02:54.650 --> 00:02:56.330
and here we can pass in the plot,

78
00:02:56.330 --> 00:02:57.680
which is lines in our case,

79
00:02:57.680 --> 00:03:00.100
and then we can also change certain parameters.

80
00:03:00.100 --> 00:03:01.600
Here I'm gonna pass in colour,

81
00:03:01.600 --> 00:03:03.710
making use of the American spelling,

82
00:03:03.710 --> 00:03:07.440
and we can pass in a hexadecimal colour in there if we want

83
00:03:07.440 --> 00:03:10.383
and that allows us to use a custom colour if we prefer.

84
00:03:11.260 --> 00:03:13.030
As well as coordinates in the x-axis,

85
00:03:13.030 --> 00:03:14.900
we can use categorical variables.

86
00:03:14.900 --> 00:03:17.470
So here I'm going to make a couple of changes

87
00:03:17.470 --> 00:03:20.290
and instead of one, two, three and four in the x-axis,

88
00:03:20.290 --> 00:03:24.003
we're gonna plot, for example, Men, Women and Children.

89
00:03:25.010 --> 00:03:27.140
And then we can plot values against that.

90
00:03:27.140 --> 00:03:29.060
Naturally, these values will normally be coming

91
00:03:29.060 --> 00:03:31.260
from our database or some sort of file

92
00:03:31.260 --> 00:03:33.060
but here I'm just plotting arbitrary values

93
00:03:33.060 --> 00:03:34.850
but you can see that now we get these labels

94
00:03:34.850 --> 00:03:37.970
in the x-axis instead of the numbers.

95
00:03:37.970 --> 00:03:41.300
Matplotlib will automatically create even ticks,

96
00:03:41.300 --> 00:03:42.910
these things here are called ticks,

97
00:03:42.910 --> 00:03:45.360
if you rpovide numerical data.

98
00:03:45.360 --> 00:03:47.100
But if you provide categorical data,

99
00:03:47.100 --> 00:03:48.230
as we're doing here,

100
00:03:48.230 --> 00:03:50.460
then it will only show the ticks provided.

101
00:03:50.460 --> 00:03:52.780
Of course, it can't interpolate between men and women

102
00:03:52.780 --> 00:03:54.740
to give you a tick in the middle.

103
00:03:54.740 --> 00:03:55.610
As well as all this,

104
00:03:55.610 --> 00:03:57.040
which allows us to create the plot

105
00:03:57.040 --> 00:03:58.850
so we can also do a lot more.

106
00:03:58.850 --> 00:04:01.970
For example, provide labels for the x and y-axis

107
00:04:01.970 --> 00:04:04.760
or provide a title for the plot itself.

108
00:04:04.760 --> 00:04:07.330
We can do that with plt.xlabel

109
00:04:07.330 --> 00:04:09.290
and this allows us to define, for example,

110
00:04:09.290 --> 00:04:10.970
a label for the x-axis.

111
00:04:10.970 --> 00:04:14.400
Plt.ylabel allows us to define a label for the y-axis.

112
00:04:14.400 --> 00:04:16.550
I'm gonna set it to Amount here

113
00:04:16.550 --> 00:04:18.990
and plt.title allows us to define a title

114
00:04:18.990 --> 00:04:20.873
that appears above the chart.

115
00:04:23.600 --> 00:04:25.440
I'll run this to show you what it looks like

116
00:04:25.440 --> 00:04:27.260
and you can see that we've got our labels here

117
00:04:27.260 --> 00:04:29.090
at the bottom and at the left

118
00:04:29.090 --> 00:04:30.520
and our title at the top.

119
00:04:30.520 --> 00:04:31.730
All right, that's everything for this video.

120
00:04:31.730 --> 00:04:32.950
Thank you guys for joining me,

121
00:04:32.950 --> 00:04:34.453
I'll see you in the next one.

