WEBVTT

1
00:00:00.370 --> 00:00:01.750
<v ->Hi Guys, and welcome back.</v>

2
00:00:01.750 --> 00:00:04.690
In this video, we're going to learn how to draw bar charts.

3
00:00:04.690 --> 00:00:08.070
And we're going to use a polling data as a source of data.

4
00:00:08.070 --> 00:00:09.483
So let's get started.

5
00:00:10.380 --> 00:00:11.920
When we were drawing a pie chart,

6
00:00:11.920 --> 00:00:14.010
we plotted option texts

7
00:00:14.010 --> 00:00:16.050
against the sum of their votes

8
00:00:16.050 --> 00:00:19.360
so that we can create a pie chart for each poll.

9
00:00:19.360 --> 00:00:22.010
Now, we're going to be blocking every poll

10
00:00:22.010 --> 00:00:24.910
together with the total number of votes in that poll

11
00:00:24.910 --> 00:00:28.060
as a bar chart, so we have to change this query slightly

12
00:00:28.060 --> 00:00:33.060
to give us the poll name and the sum of votes.

13
00:00:33.260 --> 00:00:38.260
So we're going to select polls.title and the sum of votes.

14
00:00:39.550 --> 00:00:42.699
But now of course, we want to select from polls,

15
00:00:42.699 --> 00:00:45.083
we wanna join options.

16
00:00:46.310 --> 00:00:49.500
And down here, we're not going to set a where

17
00:00:49.500 --> 00:00:53.600
and we're gonna group by polls.name.

18
00:00:53.600 --> 00:00:56.260
Of course, we now have to change the name of our query

19
00:00:56.260 --> 00:00:59.550
to select polls and votes

20
00:00:59.550 --> 00:01:01.980
and we no longer need this query up here.

21
00:01:01.980 --> 00:01:03.850
So we're gonna get rid of that

22
00:01:03.850 --> 00:01:07.130
and just get this one there.

23
00:01:07.130 --> 00:01:09.530
Now we can go back to app.py

24
00:01:09.530 --> 00:01:11.620
and see what we need to change in here,

25
00:01:11.620 --> 00:01:13.237
our user input is going to change slightly.

26
00:01:13.237 --> 00:01:15.880
In fact, we don't have any user input anymore.

27
00:01:15.880 --> 00:01:18.810
All that we have is the creation of the graph.

28
00:01:18.810 --> 00:01:21.960
We don't need the user to tell us any information.

29
00:01:21.960 --> 00:01:23.142
So all that we'll do here

30
00:01:23.142 --> 00:01:25.084
is we're gonna get rid of everything.

31
00:01:25.084 --> 00:01:30.084
And just do charts.create bar chart

32
00:01:31.160 --> 00:01:34.280
with the database.get options.

33
00:01:34.280 --> 00:01:36.670
And of course, that does have to get renamed as well

34
00:01:36.670 --> 00:01:39.120
get polls and votes, there we go.

35
00:01:39.120 --> 00:01:41.680
That's a better function naming database.py

36
00:01:41.680 --> 00:01:42.840
we'll use that here.

37
00:01:42.840 --> 00:01:46.620
Once we've created the bar chart, we can do plt.show

38
00:01:46.620 --> 00:01:48.050
in order to display it.

39
00:01:48.050 --> 00:01:52.210
And now we can go into charts.py and add this function.

40
00:01:52.210 --> 00:01:56.410
So we'll go to charts.py, and we're gonna do this

41
00:01:56.410 --> 00:01:59.670
we're gonna get the poll information in here

42
00:01:59.670 --> 00:02:02.100
and we're gonna figure out how to create our bar chart,

43
00:02:02.100 --> 00:02:04.560
the first thing though, is to create the figure.

44
00:02:04.560 --> 00:02:07.590
So we do figure=plt.figure.

45
00:02:07.590 --> 00:02:11.830
Then we'll create our axes figure.addsubplots one, one, one.

46
00:02:11.830 --> 00:02:13.850
Notice that here we're creating two different figures.

47
00:02:13.850 --> 00:02:16.810
So it's okay to use the same coordinates for the subplot.

48
00:02:16.810 --> 00:02:18.490
In fact, that's what you should do.

49
00:02:18.490 --> 00:02:20.310
Because these will not overlap.

50
00:02:20.310 --> 00:02:23.100
They are on different canvases all together.

51
00:02:23.100 --> 00:02:25.090
Then at the end, we're gonna return figure.

52
00:02:25.090 --> 00:02:28.030
And here in the middle, we're going to draw our bar chart,

53
00:02:28.030 --> 00:02:30.530
which is axes.bar.

54
00:02:30.530 --> 00:02:32.800
Now, what do we put in there,

55
00:02:32.800 --> 00:02:36.640
we need the coordinates of each bar in the x axis.

56
00:02:36.640 --> 00:02:40.180
So that's zero, one, two, three, and so on.

57
00:02:40.180 --> 00:02:42.750
And we also need the heights of the bars.

58
00:02:42.750 --> 00:02:45.870
So here we're gonna pass in the coordinates of each bar,

59
00:02:45.870 --> 00:02:48.910
we're gonna do that with range of Len of polls,

60
00:02:48.910 --> 00:02:50.630
that is going to give us essentially,

61
00:02:50.630 --> 00:02:51.643
I'm sorry, that should be polls there,

62
00:02:51.643 --> 00:02:54.090
that that is going to give us an iterable

63
00:02:54.090 --> 00:02:56.430
that contains the numbers between zero

64
00:02:56.430 --> 00:02:58.030
and the length of polls.

65
00:02:58.030 --> 00:03:00.350
So it's gonna go, if we have 10 polls for example,

66
00:03:00.350 --> 00:03:03.930
from zero to nine, giving us 10 different coordinates

67
00:03:03.930 --> 00:03:04.990
zero, one, two, three, four,

68
00:03:04.990 --> 00:03:06.464
five, six, seven, eight, nine.

69
00:03:06.464 --> 00:03:08.990
So this is the first argument to the bar method.

70
00:03:08.990 --> 00:03:12.360
The second one is the heights of each column.

71
00:03:12.360 --> 00:03:17.360
So we're going to do poll one for poll in polls.

72
00:03:17.940 --> 00:03:21.370
And that is going to give us the heights.

73
00:03:21.370 --> 00:03:23.680
Okay, this is actually probably enough.

74
00:03:23.680 --> 00:03:26.833
So let's go back to app.py and run this.

75
00:03:29.620 --> 00:03:31.200
We actually made a small mistake here.

76
00:03:31.200 --> 00:03:34.750
We don't need a argument in here anymore.

77
00:03:34.750 --> 00:03:37.110
So I did forget to get rid of this.

78
00:03:37.110 --> 00:03:38.580
We don't need any of that anymore.

79
00:03:38.580 --> 00:03:39.830
So, sorry about that,

80
00:03:39.830 --> 00:03:41.380
let's go back and run it again.

81
00:03:42.320 --> 00:03:43.830
And another small mistake here

82
00:03:43.830 --> 00:03:46.720
back into database.py we need to group by title

83
00:03:46.720 --> 00:03:48.963
since that is the name of our column.

84
00:03:49.866 --> 00:03:51.060
Running that again,

85
00:03:51.060 --> 00:03:53.010
we get now our polls

86
00:03:53.010 --> 00:03:56.840
notice that each poll has a different number of votes in it.

87
00:03:56.840 --> 00:03:59.780
And this is a distribution of our votes.

88
00:03:59.780 --> 00:04:03.600
This is cool, but we can't see which poll the votes are for.

89
00:04:03.600 --> 00:04:07.060
So let's fix that by going over to charts.py,

90
00:04:07.060 --> 00:04:08.660
we're going to add a couple of things here.

91
00:04:08.660 --> 00:04:09.890
I'm going to start off by closing

92
00:04:09.890 --> 00:04:11.450
the project on the left though,

93
00:04:11.450 --> 00:04:13.540
since we don't really need that.

94
00:04:13.540 --> 00:04:15.070
In order to display a label,

95
00:04:15.070 --> 00:04:17.860
we need to pass in another argument tick label.

96
00:04:17.860 --> 00:04:21.790
And this has to contain a string for each column.

97
00:04:21.790 --> 00:04:25.380
So we'll do poll zero for poll, in polls

98
00:04:25.380 --> 00:04:27.410
and that is going to give us one string

99
00:04:27.410 --> 00:04:29.023
for each column that we've created.

100
00:04:29.023 --> 00:04:32.190
Now rerunning this is going to give us

101
00:04:32.190 --> 00:04:33.780
the poll titles there,

102
00:04:33.780 --> 00:04:36.690
but as you can see, not terribly easy to see,

103
00:04:36.690 --> 00:04:38.270
especially because they are long.

104
00:04:38.270 --> 00:04:39.500
So there's a few things we can do,

105
00:04:39.500 --> 00:04:41.110
we can obviously extend this

106
00:04:41.110 --> 00:04:44.280
until you can barely see them in this big screen,

107
00:04:44.280 --> 00:04:45.712
and not ideal.

108
00:04:45.712 --> 00:04:48.462
I'm sure there's something that we can do a bit better.

109
00:04:49.530 --> 00:04:51.440
The usual thing to do with these labels

110
00:04:51.440 --> 00:04:53.530
is to rotate them slightly

111
00:04:53.530 --> 00:04:57.520
so that they'll appear, you know, like this in each side,

112
00:04:57.520 --> 00:04:59.510
rather than straight and that way

113
00:04:59.510 --> 00:05:01.440
because they'll all be slightly rotated,

114
00:05:01.440 --> 00:05:03.610
they won't intersect with each other.

115
00:05:03.610 --> 00:05:05.036
However, in order to do that,

116
00:05:05.036 --> 00:05:08.990
we also need to make this area down here a little bit bigger

117
00:05:08.990 --> 00:05:11.770
so that they'll have more space when they are rotated.

118
00:05:11.770 --> 00:05:13.520
Otherwise they will end up clipped.

119
00:05:14.390 --> 00:05:15.670
So let's learn in the next video

120
00:05:15.670 --> 00:05:17.640
how to adjust this area down here,

121
00:05:17.640 --> 00:05:19.460
how to adjust the size of the plot.

122
00:05:19.460 --> 00:05:21.030
And then in the video after that,

123
00:05:21.030 --> 00:05:23.320
we'll learn how to rotate our labels.

124
00:05:23.320 --> 00:05:24.870
I'll see you in the next video.

