WEBVTT

1
00:00:01.081 --> 00:00:02.312
<v ->Hi guys, and welcome back.</v>

2
00:00:02.312 --> 00:00:04.200
In this video, we're going to learn how to draw

3
00:00:04.200 --> 00:00:07.010
a stacked bar chart using matplotlib.

4
00:00:07.010 --> 00:00:08.150
Over the last couple of videos,

5
00:00:08.150 --> 00:00:10.950
we've explored how to integrate matplotlib

6
00:00:10.950 --> 00:00:13.960
into a application that uses a database.

7
00:00:13.960 --> 00:00:16.150
Now, because we've already learned how to do that,

8
00:00:16.150 --> 00:00:18.000
I'm going to start moving away from that,

9
00:00:18.000 --> 00:00:19.850
so that I don't have to write the same code

10
00:00:19.850 --> 00:00:22.370
and queries over and over that we have been writing

11
00:00:22.370 --> 00:00:23.980
through the entire course and instead,

12
00:00:23.980 --> 00:00:27.510
we're going to focus simply on the matplotlib aspect itself.

13
00:00:27.510 --> 00:00:30.560
So for this lecture, I've given you the data,

14
00:00:30.560 --> 00:00:33.050
and you can copy that and paste it into a file

15
00:00:33.050 --> 00:00:34.180
in your project.

16
00:00:34.180 --> 00:00:37.600
I'm naming that file data.py in my case,

17
00:00:37.600 --> 00:00:39.940
and it contains some information that we'll need

18
00:00:39.940 --> 00:00:41.730
for this bar chart.

19
00:00:41.730 --> 00:00:43.310
Let's get started.

20
00:00:43.310 --> 00:00:45.510
What we've got in data.py, which is linked

21
00:00:45.510 --> 00:00:47.490
in the resources section of this lecture

22
00:00:47.490 --> 00:00:49.250
is this set of polls.

23
00:00:49.250 --> 00:00:52.930
Each poll has a name, the number of men who voted

24
00:00:52.930 --> 00:00:55.610
in this poll and the number of women who voted in this poll.

25
00:00:55.610 --> 00:00:58.130
Normally when you're collecting vote information,

26
00:00:58.130 --> 00:01:00.980
you're gonna collect more than just male or female,

27
00:01:00.980 --> 00:01:03.120
you may collect more information about gender,

28
00:01:03.120 --> 00:01:06.140
you may collect information about age, and where they live,

29
00:01:06.140 --> 00:01:08.460
et cetera, all that demographic data.

30
00:01:08.460 --> 00:01:10.720
But for this case, since we only want to show you

31
00:01:10.720 --> 00:01:13.950
how to plot a stacked bar chart, this is enough information.

32
00:01:13.950 --> 00:01:16.400
So here we've got the polls list

33
00:01:16.400 --> 00:01:19.560
and that is what we're going to import in app.py.

34
00:01:19.560 --> 00:01:23.740
So I'm gonna go ahead and do import matplotlib.pyplot as plt

35
00:01:23.740 --> 00:01:26.270
and from data import polls.

36
00:01:26.270 --> 00:01:30.300
And now we can go ahead and draw our stacked bar chart.

37
00:01:30.300 --> 00:01:32.710
We're gonna start off with just a simple bar chart,

38
00:01:32.710 --> 00:01:35.810
a normal bar chart, so we'll do poll_titles,

39
00:01:35.810 --> 00:01:38.320
we need this for the axis labels,

40
00:01:38.320 --> 00:01:43.080
poll zero for poll in polls.

41
00:01:43.080 --> 00:01:47.340
And we need the poll men, which is poll one

42
00:01:47.340 --> 00:01:49.720
for poll in polls.

43
00:01:49.720 --> 00:01:51.490
Now that we've got this, we can use that

44
00:01:51.490 --> 00:01:53.240
to create our bar chart.

45
00:01:53.240 --> 00:01:57.020
We also need the x coordinates of each bar,

46
00:01:57.020 --> 00:01:59.800
which I'm gonna create as poll x coordinates

47
00:01:59.800 --> 00:02:02.960
and this is range of len of polls.

48
00:02:02.960 --> 00:02:05.190
Now we can go ahead and use matplotlib

49
00:02:05.190 --> 00:02:08.560
to create the bar chart with figure equal plt.figure.

50
00:02:08.560 --> 00:02:12.270
The fixed size, I'm gonna set to six, six.

51
00:02:12.270 --> 00:02:14.420
Then we'll create our set of axes,

52
00:02:14.420 --> 00:02:17.090
which is figure.add sub plot.

53
00:02:17.090 --> 00:02:19.070
And then we can do axes.bar,

54
00:02:19.070 --> 00:02:23.140
and in here we'll put the x coordinates, the poll men,

55
00:02:23.140 --> 00:02:26.980
and as tick label, we'll use the poll titles.

56
00:02:26.980 --> 00:02:29.670
Then at the end, we can do plt.show.

57
00:02:29.670 --> 00:02:32.950
Nothing new here, we've essentially grouped everything

58
00:02:32.950 --> 00:02:34.830
that we know about matplotlib so far,

59
00:02:34.830 --> 00:02:36.610
and we've used it to create our bar chart.

60
00:02:36.610 --> 00:02:38.930
So let me go ahead and run this file

61
00:02:38.930 --> 00:02:42.720
and you can see that we get our bar chart here.

62
00:02:42.720 --> 00:02:45.540
Of course, we have the problem where the poll titles

63
00:02:45.540 --> 00:02:48.470
are not rotated and so forth, but we already know

64
00:02:48.470 --> 00:02:50.230
how to rotate them.

65
00:02:50.230 --> 00:02:51.600
So instead of focusing on that,

66
00:02:51.600 --> 00:02:53.940
we're going to stack our bar charts.

67
00:02:53.940 --> 00:02:56.700
The first thing we need is to get the women voters,

68
00:02:56.700 --> 00:03:01.600
so we'll do poll_women poll two for poll in polls

69
00:03:01.600 --> 00:03:05.290
and then inside the same set of axes,

70
00:03:05.290 --> 00:03:07.390
we're gonna draw another bar chart.

71
00:03:07.390 --> 00:03:11.210
So we'll say axes.bar, and again, x coordinates,

72
00:03:11.210 --> 00:03:14.940
the poll women, the tick label is going to be the same,

73
00:03:14.940 --> 00:03:18.960
which is poll titles, and we're gonna add one more thing,

74
00:03:18.960 --> 00:03:21.300
but before we add that thing, let me just run this

75
00:03:21.300 --> 00:03:22.670
as it is right now.

76
00:03:22.670 --> 00:03:25.260
And you can see that what we get here

77
00:03:25.260 --> 00:03:28.730
is something that looks like a stacked bar chart, right?

78
00:03:28.730 --> 00:03:31.270
But these three here in the middle are not stacked,

79
00:03:31.270 --> 00:03:32.103
so why not?

80
00:03:32.103 --> 00:03:35.970
What we're drawing here is we're drawing the men first

81
00:03:35.970 --> 00:03:38.210
and then the women in front if you will,

82
00:03:38.210 --> 00:03:41.010
if we are the camera, looking at this chart,

83
00:03:41.010 --> 00:03:43.290
the women are in front and they are hiding

84
00:03:43.290 --> 00:03:44.570
some of the men votes.

85
00:03:44.570 --> 00:03:48.270
So the total of this chart or what it goes up to

86
00:03:48.270 --> 00:03:52.340
is 60 because that is the higher of the two votes.

87
00:03:52.340 --> 00:03:54.510
However, for example, in the second poll,

88
00:03:54.510 --> 00:03:57.510
we only see orange because the women voted more.

89
00:03:57.510 --> 00:04:00.530
The 15 is down here, hidden behind it,

90
00:04:00.530 --> 00:04:02.290
so this is not a stacked bar chart,

91
00:04:02.290 --> 00:04:05.447
this is a pretty useless bar chart, if anything,

92
00:04:05.447 --> 00:04:06.380
so we do have to make a small change

93
00:04:06.380 --> 00:04:08.760
to put one on top of the other instead of in front

94
00:04:08.760 --> 00:04:10.298
of the other.

95
00:04:10.298 --> 00:04:11.270
And what we'll do is very straightforward,

96
00:04:11.270 --> 00:04:14.475
bottom equal poll men.

97
00:04:14.475 --> 00:04:18.620
So notice that poll_men is the values of the men votes,

98
00:04:18.620 --> 00:04:20.983
and what we're seeing in here when we're plotting

99
00:04:20.983 --> 00:04:23.722
the women votes, is that they should appear

100
00:04:23.722 --> 00:04:26.015
on top of the men votes.

101
00:04:26.015 --> 00:04:28.110
Essentially, what matplotlib is doing behind the scenes,

102
00:04:28.110 --> 00:04:32.300
is it's adding to the women votes, the men votes,

103
00:04:32.300 --> 00:04:34.300
and then it appears on top.

104
00:04:34.300 --> 00:04:37.453
So if we run this, you'll see that that is what we get.

105
00:04:38.357 --> 00:04:40.870
Now, let's learn how to adjust the x tick labels.

106
00:04:40.870 --> 00:04:43.341
We've learned how to do that earlier on,

107
00:04:43.341 --> 00:04:45.180
but we are going to do something slightly different,

108
00:04:45.180 --> 00:04:47.450
because you can see that both our plots

109
00:04:47.450 --> 00:04:50.948
have a tick_label property.

110
00:04:50.948 --> 00:04:54.297
And what happens when we do this is that the latest one

111
00:04:54.297 --> 00:04:56.000
is the one that shows up in the axes

112
00:04:56.000 --> 00:04:58.631
and in this case, they're both the same

113
00:04:58.631 --> 00:04:59.720
so we don't really mind which one shows up

114
00:04:59.720 --> 00:05:02.476
but in other situations we might.

115
00:05:02.476 --> 00:05:05.000
So it's a bit confusing to pass the tick label

116
00:05:05.000 --> 00:05:07.839
into both bar polls.

117
00:05:07.839 --> 00:05:09.079
And also, it's a bit wasteful

118
00:05:09.079 --> 00:05:10.634
because we have that duplication.

119
00:05:10.634 --> 00:05:12.210
So instead, what we can do is we can get rid of this

120
00:05:12.210 --> 00:05:17.120
and that and instead use plt.xticks to pass in

121
00:05:17.965 --> 00:05:20.391
the x coordinates, the labels as well

122
00:05:20.391 --> 00:05:22.421
as any other configuration property.

123
00:05:22.421 --> 00:05:24.662
So we'll pass in here poll x coordinates,

124
00:05:24.662 --> 00:05:28.400
we need this to map each label to one bar,

125
00:05:28.400 --> 00:05:29.990
then we'll pass in the titles.

126
00:05:29.990 --> 00:05:32.666
It's important that these two are the same length

127
00:05:32.666 --> 00:05:34.611
because the first label will be mapped

128
00:05:34.611 --> 00:05:36.725
to the first coordinate and the second label

129
00:05:36.725 --> 00:05:37.730
to the second coordinate and so on.

130
00:05:37.730 --> 00:05:39.050
Then something else that we can do

131
00:05:39.050 --> 00:05:40.760
is pass in the rotation

132
00:05:40.760 --> 00:05:44.269
and the horizontal alignment as well.

133
00:05:44.269 --> 00:05:45.910
Now let me run this again and you'll see that we get

134
00:05:45.910 --> 00:05:47.770
our bar chart like that.

135
00:05:47.770 --> 00:05:49.890
You might see that the bar chart is cut off

136
00:05:49.890 --> 00:05:51.860
at the bottom though, because that's where the subplot's

137
00:05:51.860 --> 00:05:53.120
adjust comes into play.

138
00:05:53.120 --> 00:05:56.890
Let's give our x axis ticks a bit more room with that.

139
00:05:56.890 --> 00:06:00.140
Here we can do figure.subplots adjust

140
00:06:00.140 --> 00:06:02.890
bottom equals 0.35.

141
00:06:02.890 --> 00:06:04.920
And now when we run this, we've got a bit of room

142
00:06:04.920 --> 00:06:07.570
for our labels there to show up.

143
00:06:07.570 --> 00:06:09.810
Alright, that was a bit of new information,

144
00:06:09.810 --> 00:06:12.370
with drawing the bar chart and also some review

145
00:06:12.370 --> 00:06:13.400
of old information.

146
00:06:13.400 --> 00:06:16.270
Hopefully everything's coming together nicely for you guys.

147
00:06:16.270 --> 00:06:17.470
Thank you for joining me in this video.

148
00:06:17.470 --> 00:06:20.223
Thanks for watching, and I'll see you in the next one.

