1 00:00:00,000 --> 00:00:03,449 All right. In this lecture, you'll learn 2 00:00:03,450 --> 00:00:07,200 how to make time series graphs. So we're 3 00:00:07,200 --> 00:00:09,090 talking about data where one of the 4 00:00:09,090 --> 00:00:12,660 axis, so normally the x axis consists of 5 00:00:12,690 --> 00:00:15,270 dates or times. So let's say we have 6 00:00:15,270 --> 00:00:16,920 temperature observations for several 7 00:00:16,920 --> 00:00:18,990 dates, we're going to have to pull these 8 00:00:18,990 --> 00:00:23,970 values along the x and y axis. I've got 9 00:00:23,970 --> 00:00:25,500 some nice data here. 10 00:00:25,501 --> 00:00:27,929 [No audio] 11 00:00:27,931 --> 00:00:28,931 Let's 12 00:00:28,933 --> 00:00:46,138 [No audio] 13 00:00:46,140 --> 00:00:52,952 we're going to take from this link. So it's a CSV file. 14 00:00:52,953 --> 00:00:55,604 [No audio] 15 00:00:55,605 --> 00:00:57,570 You don't have to download the 16 00:00:57,570 --> 00:00:59,520 CSV file actually, because we will be 17 00:00:59,520 --> 00:01:02,190 passing it as a link directly to the 18 00:01:02,190 --> 00:01:06,150 CSV method. So what we've got here is 19 00:01:06,644 --> 00:01:09,840 a Date column and some other attributes. 20 00:01:11,190 --> 00:01:13,620 So you may want to plot the Date along 21 00:01:13,620 --> 00:01:16,440 the x axis and then in the y axis, you 22 00:01:16,440 --> 00:01:19,860 want to show one of these features. 23 00:01:20,340 --> 00:01:23,040 Let's say this attribute Close. So I'm 24 00:01:23,040 --> 00:01:26,580 going to close this, I don't need this 25 00:01:26,580 --> 00:01:29,040 file, because I'll use a direct link. 26 00:01:30,440 --> 00:01:36,180 And let me create a new cell. So I'll be 27 00:01:36,180 --> 00:01:41,070 using again, the bokeh. plotting 28 00:01:41,070 --> 00:01:46,980 interface here. And also import pandas. 29 00:01:47,910 --> 00:01:50,310 The key here is, we will be using a 30 00:01:50,310 --> 00:01:52,380 pandas dataframe and then we will 31 00:01:52,380 --> 00:01:56,130 parse this date datetime column as dates 32 00:01:56,130 --> 00:01:59,400 so that Python reads them as dates, and 33 00:01:59,400 --> 00:02:01,110 actually is able to plot them in the x 34 00:02:01,110 --> 00:02:03,750 axis. And then we'll be using a line 35 00:02:03,750 --> 00:02:09,150 glyph to plot this data. So a line glyph 36 00:02:09,150 --> 00:02:11,700 would be, you know, you can use circles, 37 00:02:12,000 --> 00:02:14,220 triangles, etc. But you can also use 38 00:02:14,220 --> 00:02:18,540 lines. So you'd pass line here. But then 39 00:02:18,540 --> 00:02:22,350 the size doesn't make sense, because you 40 00:02:22,350 --> 00:02:27,090 may want to pass a line_width for the 41 00:02:27,360 --> 00:02:32,010 line, so not size but line_width. And if 42 00:02:32,010 --> 00:02:36,090 you execute this, you don't see circles 43 00:02:36,090 --> 00:02:38,340 anymore. So it's not a scatterplot. Now, 44 00:02:38,340 --> 00:02:42,540 but it's a line chart. So we will 45 00:02:42,540 --> 00:02:45,930 use the same concept for building this 46 00:02:46,020 --> 00:02:48,918 datetime graph, timeseries graph. 47 00:02:50,613 --> 00:02:53,970 So first thing you want to do is load the 48 00:02:53,970 --> 00:03:00,430 CSV data inside Python. So pandas.read_csv, 49 00:03:02,012 --> 00:03:05,400 and you can pass actually the link 50 00:03:06,150 --> 00:03:09,468 this link directly to the CSV method. 51 00:03:09,470 --> 00:03:14,788 [No audio] 52 00:03:14,790 --> 00:03:18,690 So how convenient is that. And then you 53 00:03:18,690 --> 00:03:22,620 want to pass the parse_dates parameter 54 00:03:22,620 --> 00:03:26,880 here, and you want to specify the name 55 00:03:26,880 --> 00:03:29,400 of the column where these dates are. And 56 00:03:29,400 --> 00:03:31,500 the name of that column was Date. So if 57 00:03:31,500 --> 00:03:33,300 you remember from the CSV file that I 58 00:03:33,300 --> 00:03:36,690 opened earlier, that was Date. So once 59 00:03:36,690 --> 00:03:39,840 you load the dataframe object, you want 60 00:03:39,840 --> 00:03:42,630 to create a figure object. Let's say 61 00:03:42,630 --> 00:03:44,504 width equals to 500. 62 00:03:44,505 --> 00:03:46,724 [No audio] 63 00:03:46,726 --> 00:03:51,702 And height, let's say 500 for now, then we 64 00:03:51,703 --> 00:03:55,976 check how it goes and we can change it later. So 65 00:03:57,163 --> 00:04:03,330 we have the x_axis_type parameter here. So 66 00:04:03,660 --> 00:04:06,570 what we should do is, you only want to 67 00:04:06,570 --> 00:04:09,300 make your axis your x axis spatial. So 68 00:04:09,300 --> 00:04:12,510 let it carry datetime datatypes. 69 00:04:12,900 --> 00:04:15,240 So to declare that you have datetimes, 70 00:04:15,270 --> 00:04:18,150 you need to pass a datatime argument as a 71 00:04:18,150 --> 00:04:22,770 string there. So that's it. Then what 72 00:04:22,770 --> 00:04:26,220 you want to do is apply the line object 73 00:04:26,310 --> 00:04:30,630 in there. So similar to this, now you 74 00:04:30,630 --> 00:04:33,450 need to pass the x axis and the y axis. 75 00:04:33,944 --> 00:04:39,074 So the x axis here should be df Date. 76 00:04:40,410 --> 00:04:43,950 And the other axis is, let's say Close. 77 00:04:43,980 --> 00:04:48,624 So the Close column. Let's make the line Orange. 78 00:04:50,069 --> 00:04:55,652 And what else, maybe a transparency alpha 0.5. 79 00:04:55,653 --> 00:05:00,069 [Author typing] 80 00:05:00,070 --> 00:05:02,075 And specify where you want to save this. 81 00:05:03,945 --> 00:05:07,211 So on the what name HTML, Timeseries.html. 82 00:05:08,730 --> 00:05:14,139 And lastly, show the plot, and let's see. 83 00:05:16,003 --> 00:05:18,586 And something is not working. So Python 84 00:05:18,587 --> 00:05:21,057 is not being able to pull all the data there. 85 00:05:21,058 --> 00:05:24,043 [No audio] 86 00:05:24,045 --> 00:05:29,415 Yeah, I need to parse the date here as a list actually. 87 00:05:29,416 --> 00:05:32,248 [No audio] 88 00:05:32,250 --> 00:05:33,250 So 89 00:05:33,252 --> 00:05:36,343 [No audio] 90 00:05:36,344 --> 00:05:40,092 yeah. Now it looks better. But it also looks a 91 00:05:40,094 --> 00:05:45,194 bit squeezed. So what you can do here is, 92 00:05:46,494 --> 00:05:50,324 you can change this to, let's say, 250. 93 00:05:50,325 --> 00:05:52,679 [No audio] 94 00:05:52,680 --> 00:05:55,851 And you'll get a better graph there. Now, if 95 00:05:55,852 --> 00:05:59,250 you want to extend it to the entire page, there's 96 00:05:59,250 --> 00:06:00,840 something you can do there. You can 97 00:06:00,840 --> 00:06:05,558 actually pass the responsive and set it to True. 98 00:06:05,559 --> 00:06:08,010 [No audio] 99 00:06:08,012 --> 00:06:09,012 Let's see. 100 00:06:09,014 --> 00:06:11,195 [No audio] 101 00:06:11,196 --> 00:06:13,726 So you have you get a bigger graph there. 102 00:06:15,597 --> 00:06:20,446 Of course, you can zoom in to see some details there. 103 00:06:21,972 --> 00:06:27,777 So for May 8, I have a value of 180, you'll get the idea. 104 00:06:27,778 --> 00:06:30,314 [No audio] 105 00:06:30,316 --> 00:06:33,402 So that's concludes this lecture. 106 00:06:34,125 --> 00:06:36,800 And I'll see you in the next one.